Skip to content

Commit 99545b0

Browse files
committed
update docs
1 parent 098353c commit 99545b0

File tree

6 files changed

+43
-15
lines changed

6 files changed

+43
-15
lines changed

docs/api/actionCreatorMiddleware.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export default function (state = {}, action: any) {
4747
import {
4848
configureStore,
4949
createActionCreatorInvariantMiddleware,
50+
MiddlewareArray,
5051
} from '@reduxjs/toolkit'
5152
import reducer from './reducer'
5253

@@ -62,6 +63,6 @@ const actionCreatorMiddleware = createActionCreatorInvariantMiddleware({
6263

6364
const store = configureStore({
6465
reducer,
65-
middleware: [actionCreatorMiddleware],
66+
middleware: new MiddlewareArray(actionCreatorMiddleware),
6667
})
6768
```

docs/api/configureStore.mdx

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ and should return a middleware array.
9494
For more details on how the `middleware` parameter works and the list of middleware that are added by default, see the
9595
[`getDefaultMiddleware` docs page](./getDefaultMiddleware.mdx).
9696

97+
:::note MiddlewareArray
98+
Typescript users are required to use a `MiddlewareArray` instance (if not using a `getDefaultMiddleware` result, which is already a `MiddlewareArray`), for better inference.
99+
100+
```ts no-transpile
101+
import { configureStore, MiddlewareArray } from '@reduxjs/toolkit'
102+
103+
configureStore({
104+
reducer: rootReducer,
105+
middleware: new MiddlewareArray(additionalMiddleware, logger),
106+
})
107+
```
108+
109+
Javascript-only users are free to use a plain array if preferred.
110+
111+
:::
112+
97113
### `devTools`
98114
99115
If this is a boolean, it will be used to indicate whether `configureStore` should automatically enable support for [the Redux DevTools browser extension](https://github.com/reduxjs/redux-devtools).
@@ -122,7 +138,7 @@ If defined as an array, these will be passed to [the Redux `compose` function](h
122138
123139
This should _not_ include `applyMiddleware()` or the Redux DevTools Extension `composeWithDevTools`, as those are already handled by `configureStore`.
124140
125-
Example: `enhancers: [offline]` will result in a final setup of `[applyMiddleware, offline, devToolsExtension]`.
141+
Example: `enhancers: new EnhancerArray(offline)` will result in a final setup of `[applyMiddleware, offline, devToolsExtension]`.
126142
127143
If defined as a callback function, it will be called with the existing array of enhancers _without_ the DevTools Extension (currently `[applyMiddleware]`),
128144
and should return a new array of enhancers. This is primarily useful for cases where a store enhancer needs to be added
@@ -131,6 +147,22 @@ in front of `applyMiddleware`, such as `redux-first-router` or `redux-offline`.
131147
Example: `enhancers: (defaultEnhancers) => defaultEnhancers.prepend(offline)` will result in a final setup
132148
of `[offline, applyMiddleware, devToolsExtension]`.
133149
150+
:::note EnhancerArray
151+
Typescript users are required to use a `EnhancerArray` instance (if not using a `getDefaultEnhancer` result, which is already a `EnhancerArray`), for better inference.
152+
153+
```ts no-transpile
154+
import { configureStore, EnhancerArray } from '@reduxjs/toolkit'
155+
156+
configureStore({
157+
reducer: rootReducer,
158+
enhancers: new EnhancerArray(offline),
159+
})
160+
```
161+
162+
Javascript-only users are free to use a plain array if preferred.
163+
164+
:::
165+
134166
## Usage
135167
136168
### Basic Example

docs/api/getDefaultMiddleware.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ If you want to customize the list of middleware, you can supply an array of midd
2828
```js
2929
const store = configureStore({
3030
reducer: rootReducer,
31-
middleware: [thunk, logger],
31+
middleware: new MiddlewareArray(thunk, logger),
3232
})
3333

3434
// Store specifically has the thunk and logger middleware applied

docs/api/immutabilityMiddleware.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export default exampleSlice.reducer
7474
import {
7575
configureStore,
7676
createImmutableStateInvariantMiddleware,
77+
MiddlewareArray,
7778
} from '@reduxjs/toolkit'
7879

7980
import exampleSliceReducer from './exampleSlice'
@@ -85,7 +86,7 @@ const immutableInvariantMiddleware = createImmutableStateInvariantMiddleware({
8586
const store = configureStore({
8687
reducer: exampleSliceReducer,
8788
// Note that this will replace all default middleware
88-
middleware: [immutableInvariantMiddleware],
89+
middleware: new MiddlewareArray(immutableInvariantMiddleware),
8990
})
9091
```
9192

docs/api/serializabilityMiddleware.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ import {
9393
configureStore,
9494
createSerializableStateInvariantMiddleware,
9595
isPlain,
96+
MiddlewareArray,
9697
} from '@reduxjs/toolkit'
9798
import reducer from './reducer'
9899

@@ -110,7 +111,7 @@ const serializableMiddleware = createSerializableStateInvariantMiddleware({
110111

111112
const store = configureStore({
112113
reducer,
113-
middleware: [serializableMiddleware],
114+
middleware: new MiddlewareArray(serializableMiddleware),
114115
})
115116
```
116117

docs/usage/usage-with-typescript.md

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,23 +136,16 @@ export default store
136136

137137
#### Using `MiddlewareArray` without `getDefaultMiddleware`
138138

139-
If you want to skip the usage of `getDefaultMiddleware` altogether, you can still use `MiddlewareArray` for type-safe concatenation of your `middleware` array. This class extends the default JavaScript `Array` type, only with modified typings for `.concat(...)` and the additional `.prepend(...)` method.
139+
If you want to skip the usage of `getDefaultMiddleware` altogether, you are requred to use `MiddlewareArray` for type-safe creation of your `middleware` array. This class extends the default JavaScript `Array` type, only with modified typings for `.concat(...)` and the additional `.prepend(...)` method.
140140

141-
This is generally not required though, as you will probably not run into any array-type-widening issues as long as you are using `as const` and do not use the spread operator.
142-
143-
So the following two calls would be equivalent:
141+
For example:
144142

145143
```ts
146144
import { configureStore, MiddlewareArray } from '@reduxjs/toolkit'
147145

148146
configureStore({
149147
reducer: rootReducer,
150-
middleware: new MiddlewareArray().concat(additionalMiddleware, logger),
151-
})
152-
153-
configureStore({
154-
reducer: rootReducer,
155-
middleware: [additionalMiddleware, logger] as const,
148+
middleware: new MiddlewareArray(additionalMiddleware, logger),
156149
})
157150
```
158151

0 commit comments

Comments
 (0)