简体   繁体   中英

Implementing Undo with redux-saga

If an action removeItem(itemId) is fired from the UI it should first delete the corresponding item from the state and then call the associated saga. If the saga fails it should reset the state to it's original state.

I was wondering if you can append information to the action payload within the reducers then fire a failed action from the saga with the same payload. This is assuming that the reducer will always be fired first (Is it true?)

You shouldn't dispatch from inside your reducer. A reducers job is to take current state, an action, and return new state from the reduction of the two. That's it. What you're trying to do can be accomplished by a strategy like the following:

  1. Use itemId to get a reference to the item and store it locally.
  2. Dispatch action to remove item from store based on id.
  3. Call associated saga.
  4. If it fails, add locally stored item from step 1 back into the store. If successful, remove reference to item from step 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM