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
* A utility function that allows defining a reducer as a mapping from action
71
81
* type to *case reducer* functions that handle these action types. The
@@ -84,8 +94,8 @@ export type CaseReducers<S, AS extends Actions> = {
84
94
* That builder provides `addCase`, `addMatcher` and `addDefaultCase` functions that may be
85
95
* called to define what actions this reducer will handle.
86
96
*
87
-
* @param initialState - The initial state that should be used when the reducer is called the first time.
88
-
* @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
89
99
* case reducers via calls to `builder.addCase(actionCreatorOrType, reducer)`.
90
100
* @example
91
101
```ts
@@ -105,7 +115,7 @@ function isActionWithNumberPayload(
* A utility function that allows defining a reducer as a mapping from action
@@ -151,7 +161,7 @@ export function createReducer<S>(
151
161
* This overload accepts an object where the keys are string action types, and the values
152
162
* are case reducer functions to handle those action types.
153
163
*
154
-
* @param initialState - The initial state that should be used when the reducer is called the first time.
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`.
155
165
* @param actionsMap - An object mapping from action types to _case reducers_, each of which handles one specific action type.
156
166
* @param actionMatchers - An array of matcher definitions in the form `{matcher, reducer}`.
157
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.
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`.
75
81
*/
76
-
initialState: State
82
+
initialState: State|(()=>State)
77
83
78
84
/**
79
85
* A mapping from action types to action-type-specific *case reducer*
0 commit comments