Skip to content

Commit fea70e4

Browse files
committed
update docs
1 parent bae8ce9 commit fea70e4

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

docs/api/createSlice.mdx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,16 @@ function createSlice({
5757
name: string,
5858
// The initial state for the reducer
5959
initialState: State,
60-
// An object of "case reducers". Key names will be used to generate actions.
61-
reducers: Record<string, ReducerFunction | ReducerAndPrepareObject>,
60+
// An object of "case reducers", or a callback that returns an object. Key names will be used to generate actions.
61+
reducers: Record<string, ReducerFunction | ReducerAndPrepareObject> | ((create: ReducerCreators<State>) => Record<string, ReducerDefinition>),
6262
// A "builder callback" function used to add more reducers
6363
extraReducers?: (builder: ActionReducerMapBuilder<State>) => void,
6464
// A preference for the slice reducer's location, used by `combineSlices` and `slice.selectors`. Defaults to `name`.
6565
reducerPath?: string,
6666
// An object of selectors, which receive the slice's state as their first parameter.
6767
selectors?: Record<string, (sliceState: State, ...args: any[]) => any>,
68+
// An object of custom slice creators, used by the reducer callback.
69+
creators?: Record<string, ReducerCreator>
6870
})
6971
```
7072

@@ -456,6 +458,14 @@ const counterSlice = createSlice({
456458

457459
:::
458460

461+
### `creators`
462+
463+
While typically [custom creators](/usage/custom-slice-creators) will be provided on a per-app basis (see [`buildCreateSlice`](#buildcreateslice)), this field allows for custom slice creators to be passed in per slice.
464+
465+
This is particularly useful when using a custom creator that is specific to a single slice.
466+
467+
An error will be thrown if there is a naming conflict between an app-wide custom creator and a slice-specific custom creator.
468+
459469
## Return Value
460470

461471
`createSlice` will return an object that looks like:

docs/usage/custom-slice-creators.mdx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ const { undo, redo, reset, updateTitle, togglePinned } =
4848

4949
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`](../api/createSlice#buildcreateslice).
5050

51+
:::note
52+
53+
Creators can also be [passed per slice](/api/createSlice#creators), but most creators will be useful in more than one slice - so it's recommended to pass them to `buildCreateSlice` instead.
54+
55+
:::
56+
5157
```ts title="Creator callback for reducers"
5258
import { buildCreateSlice, asyncThunkCreator, nanoid } from '@reduxjs/toolkit'
5359

@@ -166,7 +172,7 @@ The [creator definition](#creator-definitions) for `create.preparedReducer` is e
166172

167173
These creators are not included in the default `create` object, but can be added by passing them to [`buildCreateSlice`](../api/createSlice#buildcreateslice).
168174

169-
The name the creator is available under is based on the key used when calling `buildCreateSlice`. For example, to use `create.asyncThunk`:
175+
The name the creator is available under is based on the key used when calling `buildCreateSlice` (or [`createSlice`](/api/createSlice#creators)). For example, to use `create.asyncThunk`:
170176

171177
```ts
172178
import { buildCreateSlice, asyncThunkCreator } from '@reduxjs/toolkit'
@@ -464,7 +470,7 @@ Typically a creator will return a [single reducer definition](#single-definition
464470

465471
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.
466472

467-
It's passed to [`buildCreateSlice`](../api/createSlice#buildcreateslice) as part of the `creators` object, and the name used when calling `buildCreateSlice` will be the key the creator is nested under in the `create` object.
473+
It's passed to [`buildCreateSlice`](../api/createSlice#buildcreateslice) (or [`createSlice`](/api/createSlice#creators)) as part of the `creators` object, and the name used when calling `buildCreateSlice` will be the key the creator is nested under in the `create` object.
468474

469475
```ts no-transpile
470476
import { buildCreateSlice } from '@reduxjs/toolkit'

0 commit comments

Comments
 (0)