How do reducers handle async action types in Redux

0 votes
May i know How do reducers handle async action types in Redux?
9 hours ago in Laravel by Ashutosh
• 27,850 points
9 views

1 answer to this question.

0 votes

Reducers are inherently pure functions, which means they cannot directly manage async operations. Nevertheless, they can respond to dispatched actions, including those initiated before, during, or after an async process.

Common Pattern for Async Actions:

An async operation typically dispatches three types of actions:

REQUEST – before the async call starts (e.g., show loading spinner)

SUCCESS – when the async call succeeds (e.g., store data)

FAILURE – when the async call fails (e.g., show error)

Example with Thunk:

// action types

const FETCH_DATA_REQUEST = 'FETCH_DATA_REQUEST';

const FETCH_DATA_SUCCESS = 'FETCH_DATA_SUCCESS';

const FETCH_DATA_FAILURE = 'FETCH_DATA_FAILURE';

// reducer

const initialState = {

  loading: false,

  data: [],

  error: null,

};

const dataReducer = (state = initialState, action) => {

  switch (action.type) {

    case FETCH_DATA_REQUEST:

      return {

        ...state,

        loading: true,

        error: null,

      };

    case FETCH_DATA_SUCCESS:

      return {

        ...state,

        loading: false,

        data: action.payload,

      };

    case FETCH_DATA_FAILURE:

      return {

        ...state,

        loading: false,

        error: action.error,

      };

    default:

      return state;

  }

};

answered 3 hours ago by anonymous

Related Questions In Laravel

0 votes
1 answer

What do you mean by Rate Limiting?How can we access control in number of routes in an application ?

Hey kartik, Laravel includes a middleware to rate ...READ MORE

answered Mar 26, 2020 in Laravel by Niroj
• 82,840 points
1,459 views
0 votes
1 answer

How do I get HTTP Request body content in Laravel?

Hello @kartik, Inside controller inject Request object. So ...READ MORE

answered Aug 11, 2020 in Laravel by Niroj
• 82,840 points
17,331 views
0 votes
1 answer

How do I catch exceptions / missing pages in Laravel 5?

Hello @kartik, In Laravel 5 you can catch ...READ MORE

answered Aug 11, 2020 in Laravel by Niroj
• 82,840 points
2,141 views
0 votes
1 answer

How to get route action name in laravel?

Hello @kartik, To get action name, you need ...READ MORE

answered Sep 28, 2020 in Laravel by Niroj
• 82,840 points
5,067 views
0 votes
1 answer

How does Redux middleware handle async actions?

Redux middleware manages asynchronous actions by intercepting ...READ MORE

answered 3 hours ago in Node-js by anonymous
11 views
0 votes
1 answer

How does put() help in dispatching actions in Sagas?

put() is a Redux-Saga effect that allows ...READ MORE

answered 3 hours ago in Node-js by anonymous
10 views
0 votes
1 answer

How do you write a generator function in Redux-Saga?

In Redux-Saga, generator functions are used to ...READ MORE

answered 3 hours ago in Node-js by anonymous
9 views
0 votes
1 answer

How do you test a generator function in Redux-Saga?

Testing a saga means manually stepping through ...READ MORE

answered 3 hours ago in Node-js by anonymous
10 views
0 votes
1 answer

How can you resolve merge conflicts in Git?

To resolve merge conflicts in Git, follow ...READ MORE

answered Dec 17, 2024 in Laravel by Navya
153 views
0 votes
1 answer

How to get current financial year in PHP?

You can get the current financial year ...READ MORE

answered Mar 11 in Laravel by Sanvi
163 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP