简体   繁体   English

Redux在reducers与触发多个动作之间共享动作

[英]Redux Sharing action between reducers vs triggering multiple actions

For example, if I have a login reducer and a user icon reducer. 例如,如果我有登录reducer和用户图标reducer。 When I login, I want to update the user icon as well as storing the user's info. 当我登录时,我想更新用户图标以及存储用户的信息。 I am stuck between two choices: 我被困在两个选择之间:

First one is to export the USER_LOGIN action and have both login reducer and user icon reducer handle USER_LOGIN action. 第一个是导出USER_LOGIN操作,并且登录reducer和用户图标reducer处理USER_LOGIN操作。

Second approach is to have a 1 to 1 mapping between action and reducer (one type of action belongs to only one reducer). 第二种方法是在action和reducer之间进行1对1的映射(一种类型的动作只属于一个reducer)。 We have login reducer handle USER_LOGIN, then with Saga/Thunk we dispatch a side effect UPDATE_USER_ICON to the user icon reducer. 我们有登录缩减器句柄USER_LOGIN,然后使用Saga / Thunk我们向用户图标缩减器发送副作用UPDATE_USER_ICON。

Which one is a better practice ? 哪一个是更好的做法? I personally favor the second approach. 我个人赞成第二种方法。

Quoting the Redux FAQ entry on dispatching multiple actions : 在调度多个操作时引用Redux FAQ条目

There's no specific rule for how you should structure your actions. 关于如何构建行动没有具体规则。 Using an async middleware like Redux Thunk certainly enables scenarios such as dispatching multiple distinct but related actions in a row, dispatching actions to represent progression of an AJAX request, dispatching actions conditionally based on state, or even dispatching an action and checking the updated state immediately afterwards. 使用像Redux Thunk这样的异步中间件肯定会启用一些方案,例如连续调度多个不同但相关的操作,调度操作以表示AJAX请求的进展,根据状态有条件地调度操作,甚至调度操作并立即检查更新的状态然后。

In general, ask if these actions are related but independent, or should actually be represented as one action. 一般来说,询问这些行为是否相关但是独立,或者实际上应该表示为一个行动。 Do what makes sense for your own situation but try to balance the readability of reducers with readability of the action log. 做适合自己情况的事情,但尝试平衡减速器的可读性和动作日志的可读性。 For example, an action that includes the whole new state tree would make your reducer a one-liner, but the downside is now you have no history of why the changes are happening, so debugging gets really difficult. 例如,包含整个新状态树的操作会使您的reducer成为一个单行,但缺点是现在您没有发生更改的原因,因此调试变得非常困难。 On the other hand, if you emit actions in a loop to keep them granular, it's a sign that you might want to introduce a new action type that is handled in a different way. 另一方面,如果您在循环中发出操作以保持细化,则表明您可能希望引入以不同方式处理的新操作类型。

Try to avoid dispatching several times synchronously in a row in the places where you're concerned about performance. 尽量避免在您关注性能的地方连续多次同步调度。 There are a number of addons and approaches that can batch up dispatches as well. 有许多插件和方法也可以批量调度。

I would say your first approach is better and that is the one I use commonly. 我会说你的第一种方法更好,这是我常用的方法。 Yes it may affect readability of your code when you multiple reducers are acting upon same action type but it helps keep code less verbose (which is my major complain with React ecosystem) and a bit of performance. 是的,当多个Reducer采用相同的操作类型时,它可能会影响代码的可读性,但它有助于保持代码不那么冗长(这是我对React生态系统的主要抱怨)和一点性能。 Actions like login would not have huge impact on performance but when I am making API calls on user actions, I just handle same action type in multiple reducers. 像登录这样的操作不会对性能产生巨大影响,但是当我对用户操作进行API调用时,我只是在多个reducers中处理相同的操作类型。 For readability, I add comments and documentation. 为了便于阅读,我添加了注释和文档。

Do you really need to store the user icon in Redux? 你真的需要在Redux中存储用户图标吗? A better approach in this case is to have a selector that returns the icon based on the logged in user. 在这种情况下,更好的方法是使用一个选择器,根据登录用户返回图标。 That way you can keep the minimal amount of state in the store. 这样你就可以在商店中保持最小的状态。

You can use the Reselect library to cache selectors. 您可以使用Reselect库来缓存选择器。

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

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