Skip to content

Commit 7e7fedc

Browse files
author
ben.durrant
committed
Update enhancer docs
1 parent cf99cbd commit 7e7fedc

File tree

4 files changed

+75
-13
lines changed

4 files changed

+75
-13
lines changed

docs/api/autoBatchEnhancer.mdx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,9 @@ const counterSlice = createSlice({
4848
})
4949
const { incrementBatched, decrementUnbatched } = counterSlice.actions
5050

51+
// includes batch enhancer by default, as of RTK 2.0
5152
const store = configureStore({
5253
reducer: counterSlice.reducer,
53-
// highlight-start
54-
enhancers: (getDefaultEnhancers) => {
55-
// Add the autobatch enhancer to the store setup
56-
return getDefaultEnhancers().concat(autoBatchEnhancer())
57-
},
58-
// highlight-end
5954
})
6055
```
6156

@@ -74,6 +69,25 @@ type AutoBatchOptions =
7469
export type autoBatchEnhancer = (options?: AutoBatchOptions) => StoreEnhancer
7570
```
7671
72+
:::tip
73+
As of RTK 2.0, the `autoBatchEnhancer` is included by default when calling `configureStore`.
74+
75+
This means to configure it, you should instead pass an callback that receives `getDefaultEnhancers` and calls it with your desired settings.
76+
77+
```ts title="Configuring autoBatchEnhancer with getDefaultEnhancers"
78+
import { configureStore } from '@reduxjs/toolkit'
79+
80+
const store = configureStore({
81+
reducer: () => 0,
82+
enhancers: (getDefaultEnhancers) =>
83+
getDefaultEnhancers({
84+
autoBatch: { type: 'tick' },
85+
}),
86+
})
87+
```
88+
89+
:::
90+
7791
Creates a new instance of the autobatch store enhancer.
7892

7993
Any action that is tagged with `action.meta[SHOULD_AUTOBATCH] = true` will be treated as "low-priority", and a notification callback will be queued. The enhancer will delay notifying subscribers until either:
@@ -140,4 +154,4 @@ This allows Redux users to selectively tag certain actions for effective batchin
140154
141155
### RTK Query and Batching
142156
143-
RTK Query already marks several of its key internal action types as batchable. If you add the `autoBatchEnhancer` to the store setup, it will improve the overall UI performance, especially when rendering large lists of components that use the RTKQ query hooks.
157+
RTK Query already marks several of its key internal action types as batchable. By adding the `autoBatchEnhancer` to the store setup, it improves the overall UI performance, especially when rendering large lists of components that use the RTKQ query hooks.

docs/api/configureStore.mdx

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ If it is an object of slice reducers, like `{users : usersReducer, posts : posts
7777
7878
### `middleware`
7979
80-
An optional array of Redux middleware functions
80+
An optional array of Redux middleware functions, or a callback to customise the array of middleware.
8181
8282
If this option is provided, it should contain all the middleware functions you
8383
want added to the store. `configureStore` will automatically pass those to `applyMiddleware`.
@@ -118,17 +118,58 @@ An optional array of Redux store enhancers, or a callback function to customize
118118
119119
If defined as an array, these will be passed to [the Redux `compose` function](https://redux.js.org/api/compose), and the combined enhancer will be passed to `createStore`.
120120
121-
This should _not_ include `applyMiddleware()` or the Redux DevTools Extension `composeWithDevTools`, as those are already handled by `configureStore`.
121+
:::tip Dev Tools
122+
This should _not_ include the Redux DevTools Extension `composeWithDevTools`, as this is already handled by `configureStore`.
122123
123-
Example: `enhancers: [offline]` will result in a final setup of `[applyMiddleware, offline, devToolsExtension]`.
124+
Example: `enhancers: [offline]` will result in a final setup of `[offline, devToolsExtension]`.
125+
:::
124126
125-
If defined as a callback function, it will be called with the existing array of enhancers _without_ the DevTools Extension (currently `[applyMiddleware]`),
126-
and should return a new array of enhancers. This is primarily useful for cases where a store enhancer needs to be added
127-
in front of `applyMiddleware`, such as `redux-first-router` or `redux-offline`.
127+
If not provided, `configureStore` will call `getDefaultEnhancers` and use the array of enhancers it returns (including `applyMiddleware` with specified middleware).
128+
129+
Where you wish to add onto or customize the default enhancers, you may pass a callback function that will receive `getDefaultEnhancers` as its argument, and should return an enhancer array.
128130
129131
Example: `enhancers: (defaultEnhancers) => defaultEnhancers.prepend(offline)` will result in a final setup
130132
of `[offline, applyMiddleware, devToolsExtension]`.
131133
134+
For more details on how the `enhancer` parameter works and the list of enhancers that are added by default, see the [`getDefaultEnhancers` docs page](./getDefaultEnhancers).
135+
136+
:::caution Middleware
137+
138+
In order to use any [middleware](#middleware) specified, you _need_ to use the `getDefaultEnhancers` callback, which will include the built `applyMiddleware` enhancer.
139+
140+
If you provide an array, this `applyMiddleware` enhancer will _not_ be used.
141+
142+
`configureStore` will warn in console if any middleware are provided (or left as default) but not included in the final list of enhancers.
143+
144+
```ts no-transpile
145+
// warns - middleware left as default but not included in final enhancers
146+
configureStore({
147+
reducer,
148+
enhancers: [offline],
149+
})
150+
// warns - middleware customised but not included in final enhancers
151+
configureStore({
152+
reducer,
153+
middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(logger)
154+
enhancers: [offline],
155+
})
156+
157+
// fine - default enhancers included
158+
configureStore({
159+
reducer,
160+
enhancers: (getDefaultEnhancers) => getDefaultEnhancers().concat(offline),
161+
})
162+
163+
// also allowed
164+
configureStore({
165+
reducer,
166+
middleware: [],
167+
enhancers: [offline],
168+
})
169+
```
170+
171+
:::
172+
132173
## Usage
133174
134175
### Basic Example

docs/api/getDefaultEnhancers.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
id: getDefaultEnhancers
3+
title: getDefaultEnhancers
4+
sidebar_label: getDefaultEnhancers
5+
hide_title: true
6+
---

website/sidebars.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"api/actionCreatorMiddleware",
4545
"api/createListenerMiddleware",
4646
"api/createDynamicMiddleware",
47+
"api/getDefaultEnhancers",
4748
"api/autoBatchEnhancer"
4849
]
4950
},

0 commit comments

Comments
 (0)