Redux 常见问题:组织 State​

Redux 常见问题:组织 State​

Redux 常见问题:组织 State ​目录 ​必须将所有 state 都维护在 Redux 中吗? 可以用 React 的 setState() 方法吗?可以将 store 的 state 设置为函数、promise 或者其它非序列化值吗?如何在 state 中组织嵌套及重复数据?组织 State ​

必须将所有 state 都维护在 Redux 中吗? 可以用 React 的 setState() 方法吗? ​没有 “标准”。有些用户选择将所有数据都在 Redux 中维护,那么在任何时刻,应用都是完全有序及可控的。也有人将类似于“下拉菜单是否打开”的非关键或者 UI 状态,在组件内部维护。适合自己的才是最好的。

使用局部组件状态是更好的。作为一名开发者,应该决定使用何种 state 来组装你的应用,每个 state 的生存范围是什么。在两者之间做好平衡,然后就去做吧。

这里有一些将怎样的数据放入 Redux 的经验法则:

应用的其他部分是否关心这个数据?是否需要根据需要在原始数据的基础上创建衍生数据?相同的数据是否被用作驱动多个组件?能否将状态恢复到特定时间点(在时光旅行调试的时候)?是否要缓存数据(比如:数据存在的情况下直接去使用它而不是重复去请求他)?有许多开源组件实现了各式各样在 Redux store 存储独立组件状态的替代方法,比如 redux-ui、 redux-component、 redux-react-local等等。还可以将 Redux 的原则和 reducers 的概念应用到组件层面,按照 this.setState((previousState) => reducer(previousState, someAction)) 的情形。

补充资料 ​文章

You Might Not Need ReduxFinding state’s place with React and Redux.A Case for setStateHow to handle state in React. The missing FAQ. Where to Hold React Component Data: state, store, static, and thisThe 5 Types Of React Application State讨论

#159: Investigate using Redux for pseudo-local component state#1098: Using Redux in reusable React component#1287: How to choose between Redux's store and React's state?#1385: What are the disadvantages of storing all your state in a single immutable atom?Twitter: Should I keep something in React component state?Twitter: Using a reducer to update a componentReact Forums: Redux and global state vs local stateReddit: "When should I put something into my Redux store?"Stack Overflow: Why is state all in one place, even state that isn't global?Stack Overflow: Should all component state be kept in Redux store?库

Redux Addons Catalog: Component State

可以将 store 的 state 设置为函数、promise 或者其它非序列化值吗? ​强烈推荐只在 store 中维护普通的可序列化对象、数组以及基本数据类型。虽然从 技术 层面上将非序列化项保存在 store 中是可行的,但这样会破坏 store 内容持久化和恢复能力,以及会干扰时间旅行。

如果你不关心数据持久化和时间旅行,那么完全欢迎把不可以持久化的数据放入 Redux 的 Store 中存储。最终,他是你的应用程序,如何实现完全取决于你自己。与其他很多 Redux 的事情一样,你需要明白权衡所需。

补充资料 ​讨论

#1248: Is it ok and possible to store a react component in a reducer?#1279: Have any suggestions for where to put a Map Component in Flux?#1390: Component Loading#1407: Just sharing a great base class#1793: React Elements in Redux State

如何在 state 中组织嵌套及重复数据? ​当数据存在 ID、嵌套或者关联关系时,应当以 “范式化” 形式存储:对象只能存储一次,ID 作为键值,对象间通过 ID 相互引用。将 store 类比于数据库,每一项都是独立的 “表”。normalizr 、 redux-orm 此类的库能在管理规范化数据时提供参考和抽象。

补充资料 ​文档

Advanced: Async ActionsExamples: Real World exampleRecipes: Structuring Reducers - Prerequisite ConceptsRecipes: Structuring Reducers - Normalizing State ShapeExamples: Tree View文章

High-Performance Reduxhttps://medium.com/@adamrackis/querying-a-redux-store-37db8c7f3b0f讨论

#316: How to create nested reducers?#815: Working with Data Structures#946: Best way to update related state fields with split reducers?#994: How to cut the boilerplate when updating nested entities?#1255: Normalizr usage with nested objects in React/Redux#1269: Add tree view example#1824: Normalising state and garbage collectionTwitter: state shape should be normalizedStack Overflow: How to handle tree-shaped entities in Redux reducers?Stack Overflow: How to optimize small updates to props of nested components in React + Redux?

你可能也喜欢

无线投屏
365bet官网平台网址

无线投屏

📅 06-27 👀 2482
湛江市赤坎区百姓村:正月十七年例!万人宴,民俗大巡游
beat365手机版官方网站正规

湛江市赤坎区百姓村:正月十七年例!万人宴,民俗大巡游

📅 06-28 👀 3998
【蘋果隱私】iCloud帳戶被盜用!用戶私密照外流,我該如何預防?
《小咖秀》视频无法正常观看的解决办法
怎样获得免费office365

《小咖秀》视频无法正常观看的解决办法

📅 06-27 👀 650
12部高分欧美硬汉动作大片,肾上腺素狂飙到爆!
beat365手机版官方网站正规

12部高分欧美硬汉动作大片,肾上腺素狂飙到爆!

📅 07-01 👀 2223
毕业两年后我是如何攒到十五万的
beat365手机版官方网站正规

毕业两年后我是如何攒到十五万的

📅 06-30 👀 9242