在 react 和 redux 中处理本地状态 [英] Dealing with local state in react and redux

查看:28
本文介绍了在 react 和 redux 中处理本地状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

react 和 redux 一起使用时,是否可以将本地状态存储在 state 对象中?通过操作将所有内容存储在状态树中很快就会变得乏味.感觉某些状态仅与应用程序的呈现/显示相关,而不是与逻辑相关.我所说的演示是指动画/闪烁、面板的展开/收缩状态、表格中的排序标准等.

Is it OK to store local state in the state object when using react together with redux? Storing everything in the state tree via actions quickly becomes tedious. It feels that some state is relevant only for presentation/display of the app, rather than the logic. By presentation I mean animations/blinking, the expanded/contracted state of panels, the sorting criteria in tables and so on.

推荐答案

这很难回答,因为不同的人会将组件的不同部分归类为状态".

This is difficult to answer, because different people will classify different parts of a component as "state".

由于 Redux 与应用程序状态有关,因此根据经验,您希望应用程序级别的撤消/重做"按钮生效的任何事情都应该作为 Redux 操作发生.Redux 有一个撤消存储插件的事实只是因为应用程序状态的可达性.

Since Redux is concerned with application state, as a rule of thumb, anything you would expect an application level "undo/redo" button to effect should happen as a Redux Action. The fact that Redux has an undo store plugin is possible only because of the reach of application state.

当然有些动画是不可撤销的,因为这些动画应该真正与应用程序状态的变化相关,而不是本身的变化.不过,您的其余示例听起来非常像应用程序状态.如果我对表格进行排序,然后按下撤消,我绝对希望排序会被撤消.

Certainly some animations would not be undoable, since these should really be connected to changes in the app state, not changes in-and-of themselves. The rest of your examples, though, sound very much like app state. If I sorted a table, and then pressed undo, I would absolutely expect the sorting to be undone.

这篇关于在 react 和 redux 中处理本地状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆