How to update Redux state in response to async actions using immer

0 votes
Can i know How to update Redux state in response to async actions using immer?
2 days ago in Node-js by Ashutosh
• 22,810 points
22 views

1 answer to this question.

0 votes

To update Redux state in response to async actions using immer:

Install Immer:

npm install immer

Import produce from Immer:

import produce from 'immer';

Use in Reducer:

const initialState = {

  data: [],

  loading: false,

  error: null,

};

const reducer = (state = initialState, action) =>

  produce(state, draft => {

    switch (action.type) {

      case 'FETCH_REQUEST':

        draft.loading = true;

        draft.error = null;

        break;

      case 'FETCH_SUCCESS':

        draft.loading = false;

        draft.data = action.payload;

        break;

      case 'FETCH_FAILURE':

        draft.loading = false;

        draft.error = action.payload;

        break;

    }

  });

answered 8 hours ago by Anvi

Related Questions In Node-js

0 votes
0 answers
0 votes
0 answers

How to enhance async operations in Redux using middleware?

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

4 days ago in Node-js by Nidhi
• 12,380 points
25 views
0 votes
0 answers

How to implement action creators in Redux for async actions?

With the help  of code can you ...READ MORE

4 days ago in Node-js by Nidhi
• 12,380 points
44 views
0 votes
0 answers

How to update state based on async action outcomes in reducers?

Can i know How to update state ...READ MORE

4 days ago in Node-js by Nidhi
• 12,380 points
51 views
0 votes
1 answer

How to run an HTML file using Node.js?

1.Install Node.js 2.Create a Project Folder mkdir html-node-app cd html-node-app 3.Initialize ...READ MORE

answered Feb 12 in Node-js by Navya
68 views
0 votes
1 answer

How can I dynamically validate Angular forms based on user input?

Dynamic Form Controls with Validation: In scenarios where ...READ MORE

answered Feb 12 in Angular by Navya
79 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to use middleware for logging actions and state changes in Redux?

To use middleware for logging actions and ...READ MORE

answered 8 hours ago in Node-js by Anvi
25 views
0 votes
1 answer

how to manage complex redux state for different async calls?

To manage complex Redux state for different ...READ MORE

answered 8 hours ago in Node-js by Anvi
17 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