javascript - 为什么将同步状态放在 redux store 中?

标签 javascript reactjs redux single-page-application flux

将非异步状态放入 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/

相关文章:

javascript - 需要一些关于使用 React 的动态表单的建议

javascript - 如果 React 组件的 props 更新但值没有改变,它会重新渲染吗?

javascript - 为什么 jqPlot 轴刻度文本标签被裁剪/损坏并且轴标签不显示?

javascript - 无法将事件监听器附加到 Canvas ?

javascript - 如何在页面刷新或再次打开页面后保持字体大小始终为 150%?

javascript - 如何偏移 moment.js 时间对象

javascript - 如何避免无用的mapStateToProps()?

javascript - 检查元素在 div 中是否可见

javascript - 通过屏幕组件中的 props 将组件传递给导航屏幕

javascript - typescript |与端点的接口(interface)