将非异步状态放入 redux store 有什么意义?例如你有一个模式,显示或不显示,你想写这么多只是为了切换?只把它作为本地状态放在 react 组件中,并使用 setState 更新它有什么问题?
我决定哪个状态应该通过 redux 的经验法则是数据是异步的,否则商店中的 ui 状态那么多,有一天会变得这么大,你怎么看?
最佳答案
我将引用 the Redux FAQ entry on Redux state vs component state 中的“何时将数据放入 Redux 的经验法则” :
Some common rules of thumb for determining what kind of data should be put into Redux:
- Do other parts of the application care about this data?
- Do you need to be able to create further derived data based on this original data?
- Is the same data being used to drive multiple components?
- Is there value to you in being able to restore this state to a given point in time (ie, time travel debugging)?
- Do you want to cache the data (ie, use what's in state if it's already there instead of re-requesting it)?
在 Redux 中保持 UI 状态是完全正确的。事实上,I wrote an entire blog post demonstrating ways to store UI state like modals and toasts in Redux .
关于javascript - 为什么将同步状态放在 redux store 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52458018/