结构化 reducer
¥Structuring Reducers
从本质上讲,Redux 实际上是一个相当简单的设计模式:所有 "write" 逻辑都放入一个函数中,运行该逻辑的唯一方法是为 Redux 提供一个描述已发生事件的普通对象。Redux store 调用写入逻辑函数并传入当前状态树和描述性对象,写入逻辑函数返回一些新的状态树,Redux store 通知任何订阅者状态树已更改。
¥At its core, Redux is really a fairly simple design pattern: all your "write" logic goes into a single function, and the only way to run that logic is to give Redux a plain object that describes something that has happened. The Redux store calls that write logic function and passes in the current state tree and the descriptive object, the write logic function returns some new state tree, and the Redux store notifies any subscribers that the state tree has changed.
Redux 对写入逻辑函数的工作方式设置了一些基本约束。如 "Redux 基础知识" 第 3 部分:状态、操作和 reducer 中所述,它必须具有 (previousState, action) => newState 的签名,称为 reducer 函数,并且必须是纯粹且可预测的。
¥Redux puts some basic constraints on how that write logic function should work. As described in "Redux Fundamentals" Part 3: State, Actions, and Reducers, it has to have a signature of (previousState, action) => newState, is known as a reducer function, and must be pure and predictable.
除此之外,Redux 并不真正关心你如何在该 reducer 函数中实际构建逻辑,只要它遵守这些基本规则即可。这既是自由的源泉,也是混乱的源泉。然而,在编写 reducer 时有许多广泛使用的常见模式,以及需要注意的许多相关主题和概念。随着应用的增长,这些模式在管理 reducer 代码复杂性、处理实际数据和优化 UI 性能方面发挥着至关重要的作用。
¥Beyond that, Redux does not really care how you actually structure your logic inside that reducer function, as long as it obeys those basic rules. This is both a source of freedom and a source of confusion. However, there are a number of common patterns that are widely used when writing reducers, as well as a number of related topics and concepts to be aware of. As an application grows, these patterns play a crucial role in managing reducer code complexity, handling real-world data, and optimizing UI performance.
编写 Reducer 的先决概念
¥Prerequisite Concepts for Writing Reducers
其中一些概念已经在 Redux 文档的其他地方进行了描述。其他概念是通用的,适用于 Redux 本身之外,并且有许多现有文章详细介绍了这些概念。这些概念和技术构成了编写可靠的 Redux reducer 逻辑的基础。
¥Some of these concepts are already described elsewhere in the Redux documentation. Others are generic and applicable outside of Redux itself, and there are numerous existing articles that cover these concepts in detail. These concepts and techniques form the foundation of writing solid Redux reducer logic.
在继续使用更高级和特定于 Redux 的技术之前,彻底理解这些先决概念至关重要。推荐的阅读清单可在以下位置找到:
¥It is vital that these Prerequisite Concepts are thoroughly understood before moving on to more advanced and Redux-specific techniques. A recommended reading list is available at:
先决条件概念
标准 Redux 架构依赖于使用纯 JS 对象和数组来表示状态。如果你出于某种原因使用替代方法,则详细信息可能会因你的方法而异,但许多原则仍然适用。
¥Standard Redux architecture relies on using plain JS objects and arrays for your state. If you're using an alternate approach for some reason, the details may differ based on your approach, but many of the principles will still apply.
Reducer 概念和技术
¥Reducer Concepts and Techniques