Open In App

What is Redux Thunk?

Last Updated : 04 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Redux Thunk is like a co-worker for Redux, giving it the power to handle asynchronous actions. It’s that extra tool that allows your Redux store to deal with things like fetching data from a server or performing tasks that take some time. With Redux Thunk, your app can smoothly manage both synchronous and asynchronous operations.

Prerequisites

What is Redux Thunk?

  • Redux Thunk is like a reliable assistant for Redux, the manager of your app’s data.
  • Imagine it as the go-to helper for handling tasks that take some time, such as checking stock in an online store. It ensures your app runs smoothly, managing both quick and more complex actions without slowing down the show.
  • In this real-life scenario, Redux Thunk allows Redux to smoothly manage the asynchronous task of checking stock without freezing the entire operation.
  • It’s like having an efficient assistant that can handle both quick tasks and more time-consuming ones, ensuring your online store runs seamlessly.

Example: Suppose you are building a weather app using React and Redux. For this app you want to display the current temperature, but fetching this data from a weather API takes some time.

Without Redux Thunk:

Javascript




// Fist is to Action Creator
const fetchTemperatureRedux = () => {
  // This won't work without Redux Thunk for asynchronous operations
  return {
    type: 'FETCH_TEMPERATURE',
    payload: fetch('https://api.weather.com/current-temperature')
  };
};


With Redux Thunk:

Javascript




// Action Creator using Redux Thunk
const fetchTemperatureRedux = () => {
  return async (dispatch) => {
    // Now you can perform asynchronous operations with Redux Thunk
    try {
    // Let's take one example URL
      const response = await fetch('https://api.weather.com/current-temperature');
      const data = await response.json();
 
      dispatch({
        type: 'FETCH_TEMPERATURE',
        payload: data.temperature
      });
    } catch (error) {
      dispatch({
        type: 'FETCH_ERROR',
        payload: 'Failed to fetch temperature data'
      });
    }
  };
};


In the first example without Redux Thunk, the action creator returns an object, but it won’t handle asynchronous operations like fetching the current temperature.

In the second example with Redux Thunk, the action creator returns a function. This function receives dispatch as an argument, allowing you to perform asynchronous operations (like fetching data) before dispatching the actual action. Redux Thunk enables you to handle real-world scenarios, like fetching data from an API and gracefully dealing with errors.

Advantages of React Thunk:

  • Asynchronous Operations: Redux Thunk enables handling asynchronous tasks seamlessly within Redux, like fetching data from an API, ensuring smooth integration with the state management.
  • Flexibility with Actions: It allows the dispatching of actions that are functions, providing greater flexibility when dealing with side effects and asynchronous operations.
  • Enhanced State Management: The ability to wait for data before updating the Redux store enhances state management, facilitating more complex scenarios in web applications.

Disadvantages of React Thunk:

  • Complextiy: Introducing Redux Thunk may add complexity to the application, especially for simple projects, as it involves writing and managing asynchronous action creators.
  • Learning Curve: Developers new to Redux might face a steeper learning curve when incorporating Redux Thunk due to its asynchronous nature and the need to understand how to structure actions.
  • Potential Overhead: For small-scale projects or applications with minimal asynchronous requirements, using Redux Thunk might introduce unnecessary overhead, making it less suitable for lightweight applications.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads