Skip to content

Commit 8729add

Browse files
committed
write up docs
1 parent 31f9767 commit 8729add

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

docs/api/reduceReducers.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
---
2+
id: reducereducers
3+
title: reduceReducers
4+
hide_title: true
5+
description: 'API > reduceReducers: running multiple reducers on the same state'
6+
---
7+
8+
 
9+
10+
# `reduceReducers(initialState, ...reducers)`
11+
12+
## Overview
13+
14+
The `reduceReducers` helper function composes multiple reducer functions into one. It runs each reducer in sequence, passing the result of each to the next, and returns the final result.
15+
16+
For example, you might want a base reducer built using [`combineReducers`](./combineReducers), but then also include some logic that requires the full combined state.
17+
18+
```js
19+
const combinedReducer = combineReducers({
20+
counter: counterReducer,
21+
todos: todosReducer
22+
})
23+
24+
const rootReducer = reduceReducers(combinedReducer, otherTopLevelFeatureReducer)
25+
// or
26+
const rootReducer = reduceReducers(
27+
{ counter: 0, todos: [] },
28+
combinedReducer,
29+
otherTopLevelFeatureReducer
30+
)
31+
```
32+
33+
## Arguments
34+
35+
1. `initialState` (_any_): The initial state. This can also be the preloaded state for the reducer. This can also be omitted, and the first reducer's initial state will be used instead.
36+
37+
2. `...reducers` (_Function_): An set of reducer functions that need to be composed into one.
38+
39+
## Returns
40+
41+
(_Function_): A reducer that invokes every reducer passed in order, and returns the result of the last reducer.
42+
43+
## Example
44+
45+
#### `reducers/todos.js`
46+
47+
```js
48+
export default function todos(state = [], action) {
49+
switch (action.type) {
50+
case 'ADD_TODO':
51+
return state.concat([action.text])
52+
default:
53+
return state
54+
}
55+
}
56+
```
57+
58+
#### `reducers/counter.js`
59+
60+
```js
61+
export default function counter(state = 0, action) {
62+
switch (action.type) {
63+
case 'INCREMENT':
64+
return state + 1
65+
case 'DECREMENT':
66+
return state - 1
67+
default:
68+
return state
69+
}
70+
}
71+
```
72+
73+
#### `reducers/otherTopLevelFeature.js`
74+
75+
```js
76+
export default function otherTopLevelFeature(
77+
state = { counter: 0, todos: [] },
78+
action
79+
) {
80+
switch (action.type) {
81+
case 'COUNT_TODOS':
82+
return {
83+
...state,
84+
counter: state.todos.length
85+
}
86+
default:
87+
return state
88+
}
89+
}
90+
```
91+
92+
#### `reducers/index.js`
93+
94+
```js
95+
import { combineReducers, reduceReducers } from '@reduxjs/toolkit'
96+
import todos from './todos'
97+
import counter from './counter'
98+
import otherTopLevelFeature from './otherTopLevelFeature'
99+
100+
export default reduceReducers(
101+
combineReducers({
102+
counter,
103+
todos
104+
}),
105+
otherTopLevelFeature
106+
)
107+
```
108+
109+
#### `App.js`
110+
111+
```js
112+
import { configureStore } from '@reduxjs/toolkit'
113+
import reducer from './reducers/index'
114+
115+
const store = configureStore({
116+
reducer
117+
})
118+
console.log(store.getState())
119+
// {
120+
// counter: 0,
121+
// todos: []
122+
// }
123+
124+
store.dispatch({
125+
type: 'ADD_TODO',
126+
text: 'Use Redux'
127+
})
128+
console.log(store.getState())
129+
// {
130+
// counter: 0,
131+
// todos: [ 'Use Redux' ]
132+
// }
133+
134+
store.dispatch({
135+
type: 'COUNT_TODOS'
136+
})
137+
console.log(store.getState())
138+
// {
139+
// counter: 1,
140+
// todos: [ 'Use Redux' ]
141+
// }
142+
```
143+
144+
## Tips
145+
146+
- You may call `reduceReducers` at any level of the reducer hierarchy. It doesn't have to happen at the top. In fact you may use it again to split the child reducers that get too complicated into independent grandchildren, and so on.

website/sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ module.exports = {
152152
'api/applymiddleware',
153153
'api/bindactioncreators',
154154
'api/compose',
155+
'api/reducereducers',
155156
'api/utils',
156157
{ type: 'link', label: 'Error Messages', href: '/errors' }
157158
],

0 commit comments

Comments
 (0)