Show Loading Indicator For React Query API Call

Last updated on 27 Jul, 2021

We use useQuery hook in react-query to fetch data from an API. The query object returned by useQuery hook has helper properties to find out the status of API request.

Here is an example of fetching user data from Github.

const queryUsers = useQuery("gitUsers", () =>
  fetch("").then((res) => res.json())

queryUsers object now contains a property status which returns "loading", "error", or "success" based on the state of request.

queryUsers also has helper properties to check the state like:


Based on the value of these flags, we can show loading indicator in our React component.

--- ○ ---
Joby Joseph
Web Architect