Data fetching from APIs may take a while; therefore, it is vital to show a loader until a response is received.
API calls may be triggered by a button, an input, or simply by a page-load; however, some applications can take a few seconds to complete data fetching. If there is no visual feedback given to the users, they might think that your application is not responding or stuck.
When it comes to User Experience (UX), it is recommended to keep the user aware of the system's current state. It is always a good practice to display the loading state to the user while fetching data.
Displaying the loading state in React is very simple. We can do it using the React useState
hook, which allows us to track the state of a functional component. If you are interested to learn more about them, check out our previous guide about consuming an API in React using React Hooks.
Let's go ahead and implement the loading state in three simple steps. Take a look at the following web app, which looks up web domains using an API when a user clicks the search button.
Import the useState
hook in your file and create a state loading
, and initialize it to false
.
js
import { useState } from react;// Inside your React Componentconst [loading, setLoading] = useState(false);
To display the loader, we need to be aware of the current state of data fetching. We are going to do the following:
js
// Before calling the APIsetLoading(true);// After response is receivedsetLoading(false);
Let's add it to the domain app now. To get the loading state, we will use the function getDomainInfo
, which fetches the data from the API.
js
import { useState } from 'react';export default function Home() {const [loading, setLoading] = useState(false);const getDomainInfo = async () => {try {setLoading(true); // Set loading before sending API requestconst res = await axios.get('api/search/', {params: { keyword },});const response = res; // Response receivedsetLoading(false); // Stop loading} catch (error) {setLoading(false); // Stop loading in case of errorconsole.error(error);}};
Finally, we can display the loading state on our front end. Let's keep it simple and add it to the search button.
js
<button type="submit">{loading ? <>Loading..</> : <>Search</>}</button>
Now, the button will look like this while data is being fetched.
Similarly, you can use animations, spinners, icons, or simple text to display the loading state in your React application.