Skip to content

Commit 31c8a93

Browse files
committed
update docs
1 parent adb461d commit 31c8a93

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

docs/api/createSlice.mdx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ const todosSlice = createSlice({
138138

139139
Alternatively, the `reducers` field can be a callback which receives a "create" object.
140140

141-
The main benefit of this is that you can create [async thunks](./createAsyncThunk) as part of your slice. Types are also slightly simplified for prepared reducers.
141+
The main benefit of this is that you can create [async thunks](./createAsyncThunk) as part of your slice (though for bundle size reasons, you [need a bit of setup for this](#createasyncthunk)). Types are also slightly simplified for prepared reducers.
142142

143143
```ts title="Creator callback for reducers"
144144
import { createSlice, nanoid } from '@reduxjs/toolkit'
@@ -240,6 +240,27 @@ create.preparedReducer(
240240

241241
Creates an async thunk instead of an action creator.
242242

243+
:::warning Setup
244+
245+
To avoid pulling `createAsyncThunk` into the bundle size of `createSlice` by default, some extra setup is required to use `create.asyncThunk`.
246+
247+
The version of `createSlice` exported from RTK will throw an error if `create.asyncThunk` is called.
248+
249+
Instead, import `buildCreateSlice` and `asyncThunkCreator`, and create your own version of `createSlice`:
250+
251+
```ts
252+
import { buildCreateSlice, asyncThunkCreator } from '@reduxjs/toolkit'
253+
254+
// name is up to you
255+
export const createSliceWithThunks = buildCreateSlice({
256+
creators: { asyncThunk: asyncThunkCreator },
257+
})
258+
```
259+
260+
Then import this `createSlice` as needed instead of the exported version from RTK.
261+
262+
:::
263+
243264
**Parameters**
244265

245266
- **payloadCreator** The thunk [payload creator](./createAsyncThunk#payloadcreator).

0 commit comments

Comments
 (0)