Skip to content

Commit d3a48b8

Browse files
committed
Merge branch 'v2.0-integration' into unknown-action
2 parents 17a20c4 + a2f3c9a commit d3a48b8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+540
-410
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+
Tuple,
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 Tuple(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 Tuple
98+
Typescript users are required to use a `Tuple` instance (if not using a `getDefaultMiddleware` result, which is already a `Tuple`), for better inference.
99+
100+
```ts no-transpile
101+
import { configureStore, Tuple } from '@reduxjs/toolkit'
102+
103+
configureStore({
104+
reducer: rootReducer,
105+
middleware: new Tuple(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 Tuple(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 Tuple
151+
Typescript users are required to use a `Tuple` instance (if not using a `getDefaultEnhancer` result, which is already a `Tuple`), for better inference.
152+
153+
```ts no-transpile
154+
import { configureStore, Tuple } from '@reduxjs/toolkit'
155+
156+
configureStore({
157+
reducer: rootReducer,
158+
enhancers: new Tuple(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: 2 additions & 2 deletions
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 Tuple(thunk, logger),
3232
})
3333

3434
// Store specifically has the thunk and logger middleware applied
@@ -55,7 +55,7 @@ const store = configureStore({
5555
// Store has all of the default middleware added, _plus_ the logger middleware
5656
```
5757

58-
It is preferable to use the chainable `.concat(...)` and `.prepend(...)` methods of the returned `MiddlewareArray` instead of the array spread operator, as the latter can lose valuable type information under some circumstances.
58+
It is preferable to use the chainable `.concat(...)` and `.prepend(...)` methods of the returned `Tuple` instead of the array spread operator, as the latter can lose valuable type information under some circumstances.
5959

6060
## Included Default Middleware
6161

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+
Tuple,
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 Tuple(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+
Tuple,
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 Tuple(serializableMiddleware),
114115
})
115116
```
116117

docs/usage/usage-with-typescript.md

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export default store
9999

100100
The type of the `dispatch` function type will be directly inferred from the `middleware` option. So if you add _correctly typed_ middlewares, `dispatch` should already be correctly typed.
101101

102-
As TypeScript often widens array types when combining arrays using the spread operator, we suggest using the `.concat(...)` and `.prepend(...)` methods of the `MiddlewareArray` returned by `getDefaultMiddleware()`.
102+
As TypeScript often widens array types when combining arrays using the spread operator, we suggest using the `.concat(...)` and `.prepend(...)` methods of the `Tuple` returned by `getDefaultMiddleware()`.
103103

104104
```ts
105105
import { configureStore } from '@reduxjs/toolkit'
@@ -134,25 +134,18 @@ export type AppDispatch = typeof store.dispatch
134134
export default store
135135
```
136136

137-
#### Using `MiddlewareArray` without `getDefaultMiddleware`
137+
#### Using `Tuple` 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 required to use `Tuple` 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
146-
import { configureStore, MiddlewareArray } from '@reduxjs/toolkit'
147-
148-
configureStore({
149-
reducer: rootReducer,
150-
middleware: new MiddlewareArray().concat(additionalMiddleware, logger),
151-
})
144+
import { configureStore, Tuple } from '@reduxjs/toolkit'
152145

153146
configureStore({
154147
reducer: rootReducer,
155-
middleware: [additionalMiddleware, logger] as const,
148+
middleware: new Tuple(additionalMiddleware, logger),
156149
})
157150
```
158151

examples/publish-ci/cra4/src/mocks/handlers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export const handlers = [
3434
}),
3535

3636
rest.get('/posts/:id', (req, res, ctx) => {
37-
const { id } = req.params as { id: string }
37+
const { id: idParam } = req.params as { id: string }
38+
const id = parseInt(idParam, 10)
3839
state = adapter.updateOne(state, {
3940
id,
4041
changes: { fetched_at: new Date().toUTCString() },

examples/publish-ci/cra5/src/mocks/handlers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export const handlers = [
3434
}),
3535

3636
rest.get('/posts/:id', (req, res, ctx) => {
37-
const { id } = req.params as { id: string }
37+
const { id: idParam } = req.params as { id: string }
38+
const id = parseInt(idParam, 10)
3839
state = adapter.updateOne(state, {
3940
id,
4041
changes: { fetched_at: new Date().toUTCString() },

examples/publish-ci/next/src/mocks/handlers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export const handlers = [
3434
}),
3535

3636
rest.get('/posts/:id', (req, res, ctx) => {
37-
const { id } = req.params as { id: string }
37+
const { id: idParam } = req.params as { id: string }
38+
const id = parseInt(idParam, 10)
3839
state = adapter.updateOne(state, {
3940
id,
4041
changes: { fetched_at: new Date().toUTCString() },

examples/publish-ci/vite/src/mocks/handlers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export const handlers = [
3434
}),
3535

3636
rest.get('/posts/:id', (req, res, ctx) => {
37-
const { id } = req.params as { id: string }
37+
const { id: idParam } = req.params as { id: string }
38+
const id = parseInt(idParam, 10)
3839
state = adapter.updateOne(state, {
3940
id,
4041
changes: { fetched_at: new Date().toUTCString() },

0 commit comments

Comments
 (0)