Skip to content

Commit d826b15

Browse files
author
ben.durrant
committed
getDefaultEnhancers page
1 parent 7e7fedc commit d826b15

File tree

3 files changed

+113
-5
lines changed

3 files changed

+113
-5
lines changed

docs/api/configureStore.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,26 +145,26 @@ If you provide an array, this `applyMiddleware` enhancer will _not_ be used.
145145
// warns - middleware left as default but not included in final enhancers
146146
configureStore({
147147
reducer,
148-
enhancers: [offline],
148+
enhancers: [offline(offlineConfig)],
149149
})
150150
// warns - middleware customised but not included in final enhancers
151151
configureStore({
152152
reducer,
153153
middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(logger)
154-
enhancers: [offline],
154+
enhancers: [offline(offlineConfig)],
155155
})
156156

157157
// fine - default enhancers included
158158
configureStore({
159159
reducer,
160-
enhancers: (getDefaultEnhancers) => getDefaultEnhancers().concat(offline),
160+
enhancers: (getDefaultEnhancers) => getDefaultEnhancers().concat(offline(offlineConfig)),
161161
})
162162

163163
// also allowed
164164
configureStore({
165165
reducer,
166166
middleware: [],
167-
enhancers: [offline],
167+
enhancers: [offline(offlineConfig)],
168168
})
169169
```
170170

docs/api/getDefaultEnhancers.mdx

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,111 @@ title: getDefaultEnhancers
44
sidebar_label: getDefaultEnhancers
55
hide_title: true
66
---
7+
8+
 
9+
10+
# `getDefaultEnhancers`
11+
12+
Returns an array containing the default list of enhancers.
13+
14+
## Intended Usage
15+
16+
By default, [`configureStore`](./configureStore.mdx) adds some enhancers to the Redux store setup automatically.
17+
18+
```js
19+
const store = configureStore({
20+
reducer: rootReducer,
21+
})
22+
23+
// Store has enhancers added, because the enhancer list was not customized
24+
```
25+
26+
If you want to customise the list of enhancers, you can supply an array of enhancer functions to `configureStore`:
27+
28+
```js
29+
const store = configureStore({
30+
reducer: rootReducer,
31+
enhancers: [offline(offlineConfig)],
32+
})
33+
34+
// store specifically has the offline enhancer applied
35+
```
36+
37+
However, when you supply the `enhancer` option, you are responsible for defining _all_ the enhancers you want added
38+
to the store (with the exception of the [devtools](./configureStore#devtools)). `configureStore` will not add any extra enhancers beyond what you listed, **including the middleware enhancer**.
39+
40+
`getDefaultEnhancers` is useful if you want to add some custom enhancers, but also still want to have the default
41+
enhancers added as well:
42+
43+
```ts no-transpile
44+
import { configureStore } from '@reduxjs/toolkit'
45+
import { offline } from '@redux-offline/redux-offline'
46+
import offlineConfig from '@redux-offline/redux-offline/lib/defaults'
47+
48+
import rootReducer from './reducer'
49+
50+
const store = configureStore({
51+
reducer: rootReducer,
52+
enhancers: (getDefaultEnhancers) =>
53+
getDefaultEnhancers().concat(offline(offlineConfig)),
54+
})
55+
56+
// Store has all of the default middleware + enhancers added, _plus_ the offline enhancer
57+
```
58+
59+
## Included Default Enhancers
60+
61+
The resulting array will always contain the `applyMiddleware` enhancer created based on the `configureStore`'s `middleware` field.
62+
63+
Additionally, the [`autoBatchEnhancer`](./autoBatchEnhancer.mdx) is included, to allow for "batching" of low priority action updates. This is used by [RTK Query](/rtk-query/overview.mdx) and should improve performance when using it.
64+
65+
Currently, the return value is
66+
67+
```js
68+
const enhancers = [applyMiddleware, autoBatchEnhancer]
69+
```
70+
71+
## Customising the Included Enhancers
72+
73+
`getDefaultEnhancers` accepts an options object that allows customizing each enhancer (excluding the middleware enhancer) in two ways:
74+
75+
- Each enhancer can be excluded from the result array by passing `false` for its corresponding field
76+
- Each enhancer can have its options customized by passing the matching options object for its corresponding field
77+
78+
This example shows customising the autoBatch enhancer:
79+
80+
```ts
81+
// file: reducer.ts noEmit
82+
83+
export default function rootReducer(state = {}, action: any) {
84+
return state
85+
}
86+
87+
// file: store.ts
88+
import rootReducer from './reducer'
89+
import { configureStore } from '@reduxjs/toolkit'
90+
91+
const store = configureStore({
92+
reducer: rootReducer,
93+
enhancers: (getDefaultEnhancers) =>
94+
getDefaultEnhancers({
95+
autoBatch: { type: 'tick' },
96+
}),
97+
})
98+
```
99+
100+
## API Reference
101+
102+
```ts no-transpile
103+
interface AutoBatchOptions {
104+
// see "autoBatchEnhancer" page for options
105+
}
106+
107+
interface GetDefaultEnhancersOptions {
108+
autoBatch?: boolean | AutoBatchOptions
109+
}
110+
111+
function getDefaultEnhancers<M extends Middlewares<any>>(
112+
options: GetDefaultEnhancersOptions = {}
113+
): EnhancerArray<[StoreEnhancer<{ dispatch: ExtractDispatchExtensions<M> }>]>
114+
```

docs/api/getDefaultMiddleware.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ const middleware = [thunk]
104104

105105
`getDefaultMiddleware` accepts an options object that allows customizing each middleware in two ways:
106106

107-
- Each middleware can be excluded the result array by passing `false` for its corresponding field
107+
- Each middleware can be excluded from the result array by passing `false` for its corresponding field
108108
- Each middleware can have its options customized by passing the matching options object for its corresponding field
109109

110110
This example shows excluding the serializable state check middleware, and passing a specific value for the thunk

0 commit comments

Comments
 (0)