简体   繁体   English

使用redux-saga实现撤消

[英]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. 如果从UI触发了动作removeItem(itemId)则它应首先从状态中删除相应的项,然后调用关联的传奇。 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. 我想知道是否可以将信息附加到化简器中的动作有效载荷上,然后使用相同的有效载荷从传奇中触发failed动作。 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. 使用itemId获取对该项目的引用并将其存储在本地。
  2. Dispatch action to remove item from store based on id. 派遣行动,以根据ID从商店中移除商品。
  3. Call associated saga. 呼叫相关的传奇。
  4. If it fails, add locally stored item from step 1 back into the store. 如果失败,则将步骤1中的本地存储项重新添加到存储中。 If successful, remove reference to item from step 1. 如果成功,请从步骤1中删除对该项目的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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