ArticleZip > How To Show A Loading Indicator In React Redux App While Fetching The Data Closed

How To Show A Loading Indicator In React Redux App While Fetching The Data Closed

When you're building a React Redux app and need to fetch data from an API, it's essential to keep your users informed about the process. One way to do this is by showing a loading indicator. In this article, we'll discuss how you can easily implement a loading indicator in your React Redux app to enhance the user experience.

To begin, let's make sure you have installed the necessary packages for your project. You will need to have both React and Redux already set up in your application. If you haven't installed them yet, you can do so by running `npm install react-redux`.

Once you have the required packages in place, the next step is to create the loading indicator component. This component will be responsible for displaying a loading message or a spinner while the data is being fetched.

Here's a basic example of how you can create a LoadingIndicator component:

Jsx

import React from 'react';

const LoadingIndicator = () => {
  return (
    <div>
      <p>Loading...</p>
    </div>
  );
}

export default LoadingIndicator;

You can customize this component further by adding CSS styles or using a spinner library to make it more visually appealing.

Next, let's update your Redux store to manage the loading state. You can add a loading state to your Redux store to keep track of whether data is currently being fetched. Here's an example of how you can update your Redux store to include the loading state:

Jsx

const initialState = {
  data: [],
  loading: false,
};

const reducer = (state = initialState, action) =&gt; {
  switch (action.type) {
    case 'FETCH_DATA_REQUEST':
      return {
        ...state,
        loading: true,
      };
    case 'FETCH_DATA_SUCCESS':
      return {
        ...state,
        data: action.payload,
        loading: false,
      };
    default:
      return state;
  }
};

Now, when you make an API request to fetch data, you can dispatch an action to indicate that data is being fetched:

Jsx

const fetchData = () =&gt; {
  return (dispatch) =&gt; {
    dispatch({ type: 'FETCH_DATA_REQUEST' });
    
    // Simulate API call
    setTimeout(() =&gt; {
      const data = ['data1', 'data2', 'data3'];
      dispatch({ type: 'FETCH_DATA_SUCCESS', payload: data });
    }, 2000);
  };
};

With these changes in place, you can now update your React components to display the LoadingIndicator when the loading state is true:

Jsx

import React from 'react';
import { useSelector } from 'react-redux';
import LoadingIndicator from './LoadingIndicator';

const App = () =&gt; {
  const isLoading = useSelector(state =&gt; state.loading);

  return (
    <div>
      {isLoading ?  : }
    </div>
  );
};

export default App;

By following these steps, you can easily show a loading indicator in your React Redux app while fetching data, providing a better experience for your users.

×