• Redux 常见问题:组织 State
    • 目录
    • 组织 State
      • 必须将所有 state 都维护在 Redux 中吗? 可以用 React 的 setState() 方法吗?
        • 补充资料
      • 可以将 store 的 state 设置为函数、promise或者其它非序列化值吗?
        • 补充资料
      • 如何在 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)) 的情形。

    补充资料

    文章

    • @dan_abramov/you-might-not-need-redux-be46360cf367">You Might Not Need Redux
    • @adamrackis/finding-state-s-place-with-react-and-redux-e9a586630172#.ioh033t3j">Finding state’s place with React and Redux.
    • @zackargyle/a-case-for-setstate-1f1c47cd3f73#.dwhuf0g8f">A Case for setState
    • How to handle state in React. The missing FAQ.
    • Where to Hold React Component Data: state, store, static, and this
    • The 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 component
    • React Forums: Redux and global state vs local state
    • Reddit: “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 Actions
    • Examples: Real World example
    • Recipes: Structuring Reducers - Prerequisite Concepts
    • Recipes: Structuring Reducers - Normalizing State Shape
    • Examples: Tree View

    文章

    • High-Performance Redux
    • https://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 collection
    • Twitter: state shape should be normalized
    • Stack 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?