File tree Expand file tree Collapse file tree 1 file changed +22
-1
lines changed Expand file tree Collapse file tree 1 file changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -138,7 +138,7 @@ const todosSlice = createSlice({
138
138
139
139
Alternatively , the ` reducers ` field can be a callback which receives a " create" object .
140
140
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 .
142
142
143
143
` ` ` ts title="Creator callback for reducers"
144
144
import { createSlice, nanoid } from '@reduxjs/toolkit'
@@ -240,6 +240,27 @@ create.preparedReducer(
240
240
241
241
Creates an async thunk instead of an action creator .
242
242
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
+
243
264
** Parameters **
244
265
245
266
- ** payloadCreator ** The thunk [payload creator ](./ createAsyncThunk #payloadcreator ).
You can’t perform that action at this time.
0 commit comments