How do reducers handle async action types in Redux

0 votes
May i know How do reducers handle async action types in Redux?
Apr 24 in Laravel by Ashutosh
• 28,650 points
49 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 Apr 24 by anonymous

Related Questions In Laravel

0 votes
0 answers

How to trigger async action creators in Redux?

Can i know How to trigger async ...READ MORE

3 days ago in Laravel by Ashutosh
• 28,650 points
27 views
0 votes
0 answers

How to write an async action creator using Redux Thunk?

Can you tell me How to write ...READ MORE

3 days ago in Laravel by Ashutosh
• 28,650 points
24 views
0 votes
0 answers

What are generators in Redux-Saga and how do they work?

WIth the help of code can i ...READ MORE

3 days ago in Laravel by Ashutosh
• 28,650 points
22 views
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,481 views
0 votes
1 answer

How does Redux middleware handle async actions?

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

answered Apr 24 in Node-js by anonymous
41 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 Apr 24 in Node-js by anonymous
43 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 Apr 24 in Node-js by anonymous
43 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 Apr 24 in Node-js by anonymous
46 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
164 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
185 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