Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add a loading.gif in ReactJS

What is Loading.gif and Why Would You Use It?

To give you an analogy, imagine you're waiting for a bus. Without a clear indication of when the bus will arrive, your wait can be frustrating and confusing. Likewise, when a user interacts with your application and has to wait for a response without any visual feedback, it can lead to a poor user experience. This is where loading.gif comes into play.

Loading.gif is a simple animated image that gives users a visual cue that something is happening in the background. It's like a signal that tells the users, "Hang on, we're fetching your data." Just like a timetable at a bus stop, it makes the waiting process less frustrating and more transparent.

Now, let's dive into how you can add this to your ReactJS application.

Step 1: Choose or Create Your Loading.gif

The first step is to choose or create your loading.gif. You can find an array of pre-made loading gifs at websites like loading.io or ajaxload.info. Choose one that fits your app's style and download it. For the purpose of this tutorial, let's say we've chosen a simple spinning wheel gif and named it loading.gif.

Step 2: Import Loading.gif Into Your React Component

Once you've downloaded your gif, place it in the src directory of your project. Now, let's import it into your React component. Take a look at this code snippet:

import React from 'react';
import loadingGif from './loading.gif'; // make sure the path is correct

class MyComponent extends React.Component {
  // Your component code goes here
}

We've just imported the loading.gif file and we're ready to use it in our component.

Step 3: Display Loading.gif While Fetching Data

It's time to display our loading.gif while we're fetching data. For this, we're going to use the component's state to keep track of whether we're currently loading data or not. When we start fetching data, we'll set this state to true, and when we're done, we'll set it back to false.

Take a look at the following code:

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = { isLoading: false }; // initial state
  }

  fetchData = async () => {
    this.setState({ isLoading: true }); // start loading
    const response = await fetch('https://myapi.com/data');
    const data = await response.json();
    this.setState({ isLoading: false }); // finish loading
    // do something with data
  }

  render() {
    const { isLoading } = this.state;
    return (
      <div>
        {isLoading ? <img src={loadingGif} alt="loading..." /> : null}
        {/* Your other component code */}
      </div>
    );
  }
}

In this code, we're using the fetchData function to fetch data from an API. As soon as we start fetching, we set isLoading to true. Then, we display the loading.gif whenever isLoading is true.

Step 4: Call fetchData When Component Mounts

Finally, we want to start fetching data as soon as our component is mounted. We can do this using the componentDidMount lifecycle method. Here's how:

class MyComponent extends React.Component {
  // ...rest of the component code

  componentDidMount() {
    this.fetchData();
  }
}

Now, as soon as our component is added to the DOM, it'll start fetching data and display the loading.gif.

Conclusion

Voila! You've just added a loading.gif to your ReactJS application. Now your users will have a visual cue whenever the app is processing data, improving the overall user experience.

While our spinning wheel might not be as exciting as a bus finally appearing on the horizon, it's a small addition that can make a big difference. It's these little UI enhancements that can turn a good app into a great one.

Remember programming, like any form of creation, shouldn't be only about functionality but also about empathy. It's not just about communicating with machines but also about making humans who use your software feel understood and cared for. And sometimes, a simple spinning wheel gif can do just that.

Happy coding!