Actions and reducers are the core building blocks of Redux state management. They work together to enable a predictable, unidirectional data flow in your application. This page explains what actions and reducers are, how they work together, and the rules and patterns for using them effectively.
For information about the Redux store itself, see Store API. For details about middleware and enhancers, see Middleware and Enhancers.
Actions are plain JavaScript objects that represent "events" or "things that happened" in your application. They are the only way to send data from your application to your Redux store.
Actions must have a type property that identifies what kind of action it is. This is typically defined as a string constant. Actions may also contain additional data in a payload field or other custom fields.
Sources: docs/tutorials/fundamentals/part-1-overview.md189-197 docs/tutorials/fundamentals/part-3-state-actions-reducers.md193-211
By convention, action types are strings that describe what happened. They're typically written in a format like "domain/eventName" where
domain) represents the feature or categoryeventName) describes the specific actionIt's recommended to use strings for action types because they're serializable, which helps for debugging and tools like Redux DevTools.
Sources: docs/faq/Actions.md26-34 docs/tutorials/fundamentals/part-3-state-actions-reducers.md214-232
The Redux action objects follow a flexible structure, but there are common patterns
type field is required and should be a string that describes what happenedpayload field that contains any additional dataThe structure of the payload is up to you, though tools like Redux Toolkit and patterns like Flux Standard Action suggest using a single payload field for all action data.
Sources: docs/api/Store.md47-50 docs/tutorials/fundamentals/part-3-state-actions-reducers.md214-232
Action creators are functions that create and return action objects. They encapsulate the process of creating an action, which makes your code more reusable and easier to test.
Action creators don't have to be complex - they can simply return an action object. The real benefit is that they centralize action creation logic, making it easier to update or modify actions later.
Sources: docs/tutorials/fundamentals/part-7-standard-patterns.md40-83 docs/api/bindActionCreators.md1-113
The following diagram illustrates how actions flow through a Redux application
Sources: docs/tutorials/fundamentals/part-1-overview.md296-307 docs/tutorials/fundamentals/part-2-concepts-data-flow.md160-182
Reducers are pure functions that take the current state and an action as arguments, and return a new state. Reducers specify how the application's state changes in response to actions.
The basic signature of a reducer is
Reducers examine the action's type to determine how to update the state, and must return a new state object rather than mutating the existing state.
Sources: docs/tutorials/fundamentals/part-3-state-actions-reducers.md234-238 docs/tutorials/fundamentals/part-1-overview.md169-189
Reducers must follow these specific rules
Calculate the new state based only on the state and action arguments
Make immutable updates (no state mutations)
Handle unknown actions
Initialize state
These rules make Redux predictable and enable features like time-travel debugging.
Sources: docs/tutorials/fundamentals/part-3-state-actions-reducers.md323-353 docs/api/combineReducers.md74-85
A typical reducer is structured using a switch statement that handles different action types
While switch statements are most common, you can use any approach that properly handles different action types, including object lookups or more modern patterns available in Redux Toolkit.
Sources: docs/tutorials/fundamentals/part-3-state-actions-reducers.md287-319 docs/faq/Reducers.md15-28
Immutability is a core concept in Redux. Reducers must never mutate the state directly - they must make copies of any data they need to change.
Sources: docs/tutorials/fundamentals/part-3-state-actions-reducers.md355-402 docs/faq/ReactRedux.md52-62
Common techniques for making immutable updates
Object spread operator for objects
Array methods that return new arrays
Immutable update libraries like Immer (built into Redux Toolkit)
Remember that immutable updates require copying at every level of nesting that needs to be updated.
Sources: docs/tutorials/fundamentals/part-3-state-actions-reducers.md394-402 docs/faq/ImmutableData.md
As applications grow, you'll want to split reducers into separate functions managing different parts of the state. Redux provides combineReducers to help with this.
combineReducers takes an object where
This creates a structure where each reducer is responsible for its own slice of state, and the combined reducer creates a state object with the same shape as the reducer object.
Sources: docs/api/combineReducers.md12-53 docs/tutorials/fundamentals/part-3-state-actions-reducers.md438-449
虽然每个 reducer 都独立处理其状态片段,但所有 reducer 都会处理 action。这使得多个 reducer 可以响应同一个 action,从而在状态的不同部分实现协调更新。
如果一个 reducer 需要来自另一个状态片段的数据
getState() 访问更多状态combineReducers来源: docs/faq/Reducers.md21-30 docs/tutorials/fundamentals/part-3-state-actions-reducers.md488-505
Action 和 reducer 之间的关系构成了 Redux 单向数据流的基础
这种分离在 UI 事件、状态更改逻辑和渲染之间创建了清晰的边界。
来源: docs/tutorials/fundamentals/part-2-concepts-data-flow.md214-228 docs/faq/CodeStructure.md91-106
纯 reducer 不能包含副作用,如 API 调用或异步操作。对于这些操作,Redux 使用中间件(如 Redux Thunk)来启用异步 actions。
处理异步操作时
我们将在 中间件和增强器 中更详细地介绍此主题。
来源: docs/tutorials/fundamentals/part-6-async-logic.md1-65 docs/faq/Actions.md17-22
以下是关于 Actions 和 Reducers 的一些推荐实践
domain/eventNamepayload 中来源: docs/faq/CodeStructure.md91-106 docs/tutorials/fundamentals/part-7-standard-patterns.md40-83
现代 Redux 开发通常使用 Redux Toolkit 来简化 actions 和 reducers
createSlice 从单个配置生成 action creators 和 reducerscreateAsyncThunk 简化了异步请求模式通过 Redux Toolkit,手动创建 action types、action creators 和 switch 语句 reducers 被更简洁、更易于维护的模式所取代。
有关 Redux Toolkit 的更多信息,请参阅 使用 Redux Toolkit 进行现代 Redux 开发。
来源: docs/tutorials/fundamentals/part-8-modern-redux.md194-264 docs/tutorials/fundamentals/part-8-modern-redux.md269-344
type 字段的普通对象,用于描述“发生了什么”combineReducers 有助于将 reducers 组织到状态树中理解 actions 和 reducers 是有效使用 Redux 的基础。这些概念构成了 Redux 可预测状态管理模式的核心。
刷新此 Wiki
最后索引时间2025 年 4 月 18 日 (304031)