Skip to content

Commit

Permalink
Merge pull request #3867 from reduxjs/build-create-slice
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson authored Nov 14, 2023
2 parents 94fca95 + a385388 commit 0dc7fdd
Show file tree
Hide file tree
Showing 7 changed files with 401 additions and 195 deletions.
23 changes: 22 additions & 1 deletion docs/api/createSlice.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ const todosSlice = createSlice({

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

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

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

Creates an async thunk instead of an action creator.

:::warning Setup

To avoid pulling `createAsyncThunk` into the bundle size of `createSlice` by default, some extra setup is required to use `create.asyncThunk`.

The version of `createSlice` exported from RTK will throw an error if `create.asyncThunk` is called.

Instead, import `buildCreateSlice` and `asyncThunkCreator`, and create your own version of `createSlice`:

```ts
import { buildCreateSlice, asyncThunkCreator } from '@reduxjs/toolkit'
// name is up to you
export const createSliceWithThunks = buildCreateSlice({
creators: { asyncThunk: asyncThunkCreator },
})
```

Then import this `createSlice` as needed instead of the exported version from RTK.

:::

**Parameters**

- **payloadCreator** The thunk [payload creator](./createAsyncThunk#payloadcreator).
Expand Down
5 changes: 4 additions & 1 deletion errors.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@
"31": "\"middleware\" field must be a callback",
"32": "When using custom hooks for context, all hooks need to be provided: .\\nHook was either not provided or not a function.",
"33": "Existing Redux context detected. If you already have a store set up, please use the traditional Redux setup.",
"34": "selectSlice returned undefined for an uninjected slice reducer"
"34": "selectSlice returned undefined for an uninjected slice reducer",
"35": "Cannot use `create.asyncThunk` in the built-in `createSlice`. Use `buildCreateSlice({ creators: { asyncThunk: asyncThunkCreator } })` to create a customised version of `createSlice`.",
"36": "`context.addCase` cannot be called with an empty action type",
"37": "`context.addCase` cannot be called with two reducers for the same action type: type"
}
2 changes: 1 addition & 1 deletion packages/toolkit/src/createAsyncThunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ type CreateAsyncThunk<CurriedThunkApiConfig extends AsyncThunkConfig> = {
>
}

export const createAsyncThunk = (() => {
export const createAsyncThunk = /* @__PURE__ */ (() => {
function createAsyncThunk<
Returned,
ThunkArg,
Expand Down
Loading

0 comments on commit 0dc7fdd

Please sign in to comment.