简体   繁体   English

React-Redux:使用redux-undo改变状态以模拟动作的副作用

[英]React-Redux: Mutating the state to emulate action side-effects with redux-undo

I have a few fields that, when updated, change the href of an image (which is really an endpoint of an external API that generates a PNG based on text input). 我有几个字段,在更新时会更改图像的href(它实际上是基于文本输入生成PNG的外部API的端点)。

When that image loads, it triggers an action to update another component to match the image size (that it can only get after the image has finished loading). 加载该图像时,它会触发一个操作来更新另一个组件以匹配图像大小(只有在图像完成加载后才能获取)。 I set redux-undo to exclude that action from the history since it's not a user action. 我将redux-undo设置为从历史记录中排除该操作,因为它不是用户操作。

Here's my problem : When I do an action that does NOT trigger a change, then undo that action, the state is wrong since the size has changed in between user actions. 这是我的问题 :当我执行不触发更改的操作,然后撤消该操作时,状态是错误的,因为用户操作之间的大小已更改。

An idea I had was to mutate the state (yeesh): 我的想法是改变状态(网格):

In reducer 在减速器中

case 'UPDATE_TARGET_SIZE':
    /**
    *  HERE BE DRAGONS
    *  I am also mutating the current state since this action needs to
    *  NOT count as an user action
    */
    state.targetWidth = action.targetWidth
    state.targetHeight = action.targetHeight

    return {
        ...state
    }

While it works with no apparent drawbacks... it feels like a dirty hack. 虽然它没有明显的缺点,但感觉就像是一个肮脏的hack。 Is there another way of doing so or is it safe as long as I know why I'm mutating the state? 只要我知道为什么要改变状态,还有其他方法可以做到吗?还是安全?

Can a lib like redux-saga help me? redux-saga这样的lib可以帮助我吗? I admit I have not read its documentation since I am not making "real" API calls and I don't want to have an overkill solution. 我承认我没有阅读过它的文档,因为我没有进行“真正的” API调用,并且我不想有一个过大的解决方案。

Edit : 编辑:

Here's a better example: 这是一个更好的例子:

I type in a text input, which causes the href to change. 我输入一个文本输入,这导致href更改。 A new image is loaded, which triggers the action that is excluded from the history. 将加载一个新图像,这将触发历史记录中排除的操作。 At that point, the history does not have the modification of that last action. 在这一点上,历史记录没有最后动作的修改。

If I do an action that copies that part of the sate (in the right reducer), the state is fine. 如果我执行复制状态(在右减速器中)的那一部分的操作,则状态很好。 If I do an action that touches only another reducer, it will still be wrong. 如果我做的动作只触及另一个减速器,那仍然是错误的。

So when I press undo, I undo to the wrong state. 因此,当我按撤消时,我撤消到错误的状态。 But if I get that action to mutate the state, then it's always valid. 但是,如果我采取该行动来改变状态,那么它总是有效的。

Can a lib like redux-saga help me? 像redux-saga这样的lib可以帮助我吗? I admit I have not read its documentation since I am not making "real" API calls and I don't want to have an overkill solution. 我承认我没有阅读过它的文档,因为我没有进行“真正的” API调用,并且我不想有一个过大的解决方案。

Redux-saga allows to perform custom side effects and introduce process manager since it has own event loop. Redux-saga可以执行自定义副作用并引入流程管理器,因为它具有自己的事件循环。 Applying to original task, saga can help with splitting action with request and response in time. 将其应用于原始任务时, saga可以帮助您及时地按请求和响应拆分操作。

Typical use case is emitting action with postfix _REQUEST , like LOAD_IMAGE_REQUEST , which intercepts with saga manager and does not pass into reducers. 典型的用例是发出带有后缀_REQUEST动作,例如LOAD_IMAGE_REQUEST ,该动作被saga管理器拦截并且不传递给reducer。 Then after async operation is done, saga emits _SUCCESS or _FAILURE -like action, dependent on sucessfullity of operation. 然后,在完成异步操作后, saga会根据操作的成功程度发出_SUCCESS_FAILURE样的动作。 That actions passed into store and applied with reducers. 该动作已存储并应用到减速器中。

Also, there is ideology called optimistic update . 此外,还有一种称为optimistic update意识形态。 In that case _REQUEST event passed into store, but if action is failed, saga sents compensating event, which rollback optimistic action 在那种情况下, _REQUEST事件传递到存储中,但是如果操作失败,则saga发送补偿事件,该事件回滚乐观操作

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

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