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?
Mar 19 in Node-js by Ashutosh
• 28,250 points
95 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 Mar 21 by Anvi

Related Questions In Node-js

0 votes
1 answer
0 votes
1 answer

How to enhance async operations in Redux using middleware?

Redux-Thunk (Simple Async Operations) What it does: Allows ...READ MORE

answered Mar 18 in Node-js by Tanvi
105 views
0 votes
1 answer

How to implement action creators in Redux for async actions?

To implement action creators in Redux for ...READ MORE

answered Mar 18 in Node-js by Anvi
126 views
0 votes
1 answer

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

To manage asynchronous actions (e.g., API calls), ...READ MORE

answered Mar 18 in Node-js by Tanvi
114 views
0 votes
0 answers

How to trigger async action creators in Redux?

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

1 day ago in Laravel by Ashutosh
• 28,250 points
16 views
0 votes
0 answers

How to write an async action creator using Redux Thunk?

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

1 day ago in Laravel by Ashutosh
• 28,250 points
16 views
0 votes
0 answers

What are action creators in Redux and why use them?

With the help of code tell me ...READ MORE

1 day ago in Node-js by Ashutosh
• 28,250 points
21 views
0 votes
0 answers

What are best practices for writing clean Redux-Saga code?

Can i know What are best practices ...READ MORE

1 day ago in Laravel by Ashutosh
• 28,250 points
19 views
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 Mar 21 in Node-js by Anvi
99 views
0 votes
1 answer
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