Skip to content

Commit eaca9de

Browse files
committed
Document lazy state init option
1 parent 010bcf0 commit eaca9de

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

docs/api/createReducer.mdx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,21 @@ so we recommend the "builder callback" notation in most cases.
121121

122122
[params](docblock://createReducer.ts?token=createReducer&overload=1)
123123

124+
### Returns
125+
126+
The generated reducer function.
127+
128+
The reducer will have a `getInitialState` function attached that will return the initial state when called. This may be useful for tests or usage with React's `useReducer` hook:
129+
130+
```js
131+
const counterReducer = createReducer(0, {
132+
increment: (state, action) => state + action.payload,
133+
decrement: (state, action) => state - action.payload,
134+
})
135+
136+
console.log(counterReducer.getInitialState()) // 0
137+
```
138+
124139
### Example Usage
125140

126141
[examples](docblock://createReducer.ts?token=createReducer&overload=1)

docs/api/createSlice.mdx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ function createSlice({
7171

7272
The initial state value for this slice of state.
7373

74+
This may also be a "lazy initializer" function, which should return an initial state value when called. This will be used whenever the reducer is called with `undefined` as its state value, and is primarily useful for cases like reading initial state from `localStorage`.
75+
7476
### `name`
7577

7678
A string name for this slice of state. Generated action type constants will use this as a prefix.
@@ -196,7 +198,8 @@ We recommend using the `builder callback` API as the default, especially if you
196198
name : string,
197199
reducer : ReducerFunction,
198200
actions : Record<string, ActionCreator>,
199-
caseReducers: Record<string, CaseReducer>
201+
caseReducers: Record<string, CaseReducer>.
202+
getInitialState: () => State
200203
}
201204
```
202205

packages/toolkit/src/createReducer.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ export type ReducerWithInitialState<S extends NotFunction<any>> = Reducer<S> & {
9494
* That builder provides `addCase`, `addMatcher` and `addDefaultCase` functions that may be
9595
* called to define what actions this reducer will handle.
9696
*
97-
* @param initialState - The initial state that should be used when the reducer is called the first time.
98-
* @param builderCallback - A callback that receives a *builder* object to define
97+
* @param initialState - `State | (() => State)`: The initial state that should be used when the reducer is called the first time. This may also be a "lazy initializer" function, which should return an initial state value when called. This will be used whenever the reducer is called with `undefined` as its state value, and is primarily useful for cases like reading initial state from `localStorage`.
98+
* @param builderCallback - `(builder: Builder) => void` A callback that receives a *builder* object to define
9999
* case reducers via calls to `builder.addCase(actionCreatorOrType, reducer)`.
100100
* @example
101101
```ts
@@ -115,7 +115,7 @@ function isActionWithNumberPayload(
115115
return typeof action.payload === "number";
116116
}
117117
118-
createReducer(
118+
const reducer = createReducer(
119119
{
120120
counter: 0,
121121
sumOfNumberPayloads: 0,
@@ -161,7 +161,7 @@ export function createReducer<S extends NotFunction<any>>(
161161
* This overload accepts an object where the keys are string action types, and the values
162162
* are case reducer functions to handle those action types.
163163
*
164-
* @param initialState - The initial state that should be used when the reducer is called the first time. This may optionally be a "lazy state initializer" that returns the intended initial state value when called.
164+
* @param initialState - `State | (() => State)`: The initial state that should be used when the reducer is called the first time. This may also be a "lazy initializer" function, which should return an initial state value when called. This will be used whenever the reducer is called with `undefined` as its state value, and is primarily useful for cases like reading initial state from `localStorage`.
165165
* @param actionsMap - An object mapping from action types to _case reducers_, each of which handles one specific action type.
166166
* @param actionMatchers - An array of matcher definitions in the form `{matcher, reducer}`.
167167
* All matching reducers will be executed in order, independently if a case reducer matched or not.
@@ -174,6 +174,14 @@ const counterReducer = createReducer(0, {
174174
increment: (state, action) => state + action.payload,
175175
decrement: (state, action) => state - action.payload
176176
})
177+
178+
// Alternately, use a "lazy initializer" to provide the initial state
179+
// (works with either form of createReducer)
180+
const initialState = () => 0
181+
const counterReducer = createReducer(initialState, {
182+
increment: (state, action) => state + action.payload,
183+
decrement: (state, action) => state - action.payload
184+
})
177185
```
178186
179187
* Action creators that were generated using [`createAction`](./createAction) may be used directly as the keys here, using computed property syntax:

packages/toolkit/src/createSlice.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ export interface CreateSliceOptions<
7777
name: Name
7878

7979
/**
80-
* The initial state to be returned by the slice reducer.
81-
* This may optionally be a "lazy state initializer" that returns the
82-
* intended initial state value when called.
80+
* The initial state that should be used when the reducer is called the first time. This may also be a "lazy initializer" function, which should return an initial state value when called. This will be used whenever the reducer is called with `undefined` as its state value, and is primarily useful for cases like reading initial state from `localStorage`.
8381
*/
8482
initialState: State | (() => State)
8583

0 commit comments

Comments
 (0)