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
In order to use slice creators, `reducers` becomes a callback, which receives a `create` object. This `create` object contains a couple of [inbuilt creators](#rtk-creators), along with any creators passed to [`buildCreateSlice`](#buildcreateslice).
These creators come built into RTK, and are always available on the `create` object passed to the `reducers` callback.
117
115
118
-
#####`create.reducer`
116
+
#### `create.reducer`
119
117
120
118
A standard slice case reducer. Creates an action creator with the same name as the reducer.
121
119
@@ -135,7 +133,7 @@ The [creator definition](#creator-definitions) for `create.reducer` is exported
135
133
136
134
:::
137
135
138
-
#####`create.preparedReducer`
136
+
#### `create.preparedReducer`
139
137
140
138
A [prepared](#customizing-generated-action-creators) reducer, to customize the action creator. Creates a prepared action creator with the same name as the reducer.
141
139
@@ -164,7 +162,7 @@ The [creator definition](#creator-definitions) for `create.preparedReducer` is e
164
162
165
163
:::
166
164
167
-
####Optional RTK Creators
165
+
### Optional RTK Creators
168
166
169
167
These creators are not included in the default `create` object, but can be added by passing them to [`buildCreateSlice`](#buildcreateslice).
Creates an async thunk and adds any provided case reducers for lifecycle actions.
237
235
@@ -326,13 +324,13 @@ reducers: (create) => {
326
324
327
325
:::
328
326
329
-
###Writing your own creators
327
+
## Writing your own creators
330
328
331
329
In version v2.3.0, we introduced a system for including your own creators.
332
330
333
331
The below documentation will cover how to write your own creators, and how to use them with `createSlice`.
334
332
335
-
####Reducer definitions
333
+
### Reducer definitions
336
334
337
335
A reducer definition is an object (or function) with a `_reducerDefinitionType` property indicating which creator should handle it. Other than this property, it is entirely up to the creator what this definition object can look like.
338
336
@@ -363,7 +361,7 @@ const definitions = {
363
361
364
362
Typically a creator will return a [single reducer definition](#single-definitions), but it could return an object of [multiple definitions](#multiple-definitions) to be spread into the final object, or [something else entirely](#other)!
365
363
366
-
####Creator definitions
364
+
### Creator definitions
367
365
368
366
A creator definition contains the actual runtime logic for that creator. It's an object with a `type` property, a `create` method, and an optional `handle` method.
The `create` method is the function that will be attached to the `create` object, before it's passed to the `reducers` callback.
419
417
420
418
Because it's a function, the `this` value will be the final `create` object when called (assuming a `create.creator()` call). It also could have additional methods attached.
421
419
422
420
See the [Further examples](#further-examples) section for some examples of these.
423
421
424
-
####`handle`
422
+
### `handle`
425
423
426
424
The `handle` callback of a creator will be called for any reducer definitions with a matching `_reducerDefinitionType` property.
427
425
@@ -438,7 +436,7 @@ The reducer details object has two properties:
438
436
439
437
The context object includes:
440
438
441
-
#####`addCase`
439
+
#### `addCase`
442
440
443
441
The same as [`addCase`](./createReducer#builderaddcase) for `createReducer` and `extraReducers`. Adds a case reducer for a given action type, and can receive an action type string or an action creator with a `.type` property.
The same as [`addMatcher`](./createReducer#builderaddmatcher) for `createReducer` and `extraReducers`. Adds a case reducer which will be called when a given matcher returns true.
Attaches a value to `slice.caseReducers`. Receives the key to be set under (typically `reducerName`) and the value to be set.
471
469
472
470
```ts no-transpile
473
471
context.exposeCaseReducer(reducerName, reducer)
474
472
```
475
473
476
-
#####`getInitialState`
474
+
#### `getInitialState`
477
475
478
476
Returns the initial state value for the slice. If a lazy state initializer has been provided, it will be called and a fresh value returned.
479
477
@@ -486,7 +484,7 @@ context
486
484
.exposeCaseReducer(reducerName, resetReducer)
487
485
```
488
486
489
-
####Typescript
487
+
### Typescript
490
488
491
489
The Typescript system for custom slice creators uses a "creator registry" system similar to the module system for [RTK Query](/rtk-query/usage/customizing-create-api#creating-your-own-module).
492
490
@@ -516,7 +514,7 @@ The type parameters for `SliceReducerCreators` are:
516
514
517
515
The `ReducerCreatorEntry<Create, Exposes>` utility has two type parameters:
518
516
519
-
#####`Create`
517
+
#### `Create`
520
518
521
519
The signature of the `create` method of the creator definition.
The second type parameter for `ReducerCreatorEntry` is optional, but should be used if the creator will handle some reducer definitions itself. It indicates what actions and case reducers will be attached to the slice, and is used to determine the final types of `slice.actions` and `slice.caseReducers`.
623
621
624
622
It should be an object with some of the following properties:
625
623
626
-
######`actions`
624
+
##### `actions`
627
625
628
626
The actions property will typically be a [mapped type](https://www.typescriptlang.org/docs/handbook/2/mapped-types.html) over the `CaseReducers` type parameter, returning what the creator's `handle` would expose when given that definition.
A creator could also return multiple definitions, which would then be spread into the final definitions object. This is a more composable alternative to the [wrapping `createSlice`](usage/usage-with-typescript#wrapping-createslice) approach, as you could call multiple creators as needed.
0 commit comments