gpt4 book ai didi

reactjs - React Redux 将计算存储在reducer 或action-creator 中?

转载 作者:行者123 更新时间:2023-12-02 20:16:03 28 4
gpt4 key购买 nike

我正在通过创建一个应用程序来学习 React 和 Redux,该应用程序包含一系列基于时间的数据:允许的时间和耗时。

[1] : {
time: 10,
elapsed: 0
},
[2] : {
time: 15,
elapsed: 0
},
[3] : {
time: 20,
elapsed: 0
}

如果任何项目超出了计划,我想重新计算后续项目的时间。例如。第 1 项允许 10 秒,但如果第 1 项需要 12 秒,我想用第 2 项和第 3 项来弥补该时间。

[1] : {
time: 10,
elapsed: 12
},
[2] : {
time: 14,
elapsed: 0
},
[3] : {
time: 19,
elapsed: 0
}

我应该在哪里进行这个计算?我觉得最简单的地方就是 reducer ,但我也认为 reducer 应该尽可能简单?这是正确的吗?

那么计算应该在操作创建器中进行,然后将新的项目数组传递到商店吗?

最佳答案

引用原文Redux Documentation .

There's no single clear answer to exactly what pieces of logic should go in a reducer or an action creator. Some developers prefer to have “fat” action creators, with “thin” reducers that simply take the data in an action and blindly merge it into the corresponding state. Others try to emphasize keeping actions as small as possible, and minimize the usage of getState() in an action creator. (For purposes of this question, other async approaches such as sagas and observables fall in the "action creator" category.)

There are some potential benefits from putting more logic into your reducers. It's likely that the action types would be more semantic and more meaningful (such as "USER_UPDATED" instead of "SET_STATE"). In addition, having more logic in reducers means that more functionality will be affected by time travel debugging.

关于reactjs - React Redux 将计算存储在reducer 或action-creator 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52362921/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com