You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
constcounterReducer=createReducer(0, {
132
+
increment: (state, action) => state +action.payload,
133
+
decrement: (state, action) => state -action.payload,
Copy file name to clipboardExpand all lines: docs/api/createSlice.mdx
+4-1Lines changed: 4 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -71,6 +71,8 @@ function createSlice({
71
71
72
72
Theinitialstatevalueforthissliceofstate.
73
73
74
+
Thismayalsobea"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
+
74
76
### `name`
75
77
76
78
Astringnameforthissliceofstate. Generatedactiontypeconstants will use this as a prefix.
@@ -196,7 +198,8 @@ We recommend using the `builder callback` API as the default, especially if you
* That builder provides `addCase`, `addMatcher` and `addDefaultCase` functions that may be
95
95
* called to define what actions this reducer will handle.
96
96
*
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
99
99
* case reducers via calls to `builder.addCase(actionCreatorOrType, reducer)`.
100
100
* @example
101
101
```ts
@@ -115,7 +115,7 @@ function isActionWithNumberPayload(
115
115
return typeof action.payload === "number";
116
116
}
117
117
118
-
createReducer(
118
+
const reducer = createReducer(
119
119
{
120
120
counter: 0,
121
121
sumOfNumberPayloads: 0,
@@ -161,7 +161,7 @@ export function createReducer<S extends NotFunction<any>>(
161
161
* This overload accepts an object where the keys are string action types, and the values
162
162
* are case reducer functions to handle those action types.
163
163
*
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`.
165
165
* @param actionsMap - An object mapping from action types to _case reducers_, each of which handles one specific action type.
166
166
* @param actionMatchers - An array of matcher definitions in the form `{matcher, reducer}`.
167
167
* All matching reducers will be executed in order, independently if a case reducer matched or not.
* 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`.
0 commit comments