Skip to content

Commit bc1bf8e

Browse files
committed
Merge branch 'v2.0-integration' into v2.0-docs
2 parents f2b60e9 + a0d51ef commit bc1bf8e

Some content is hidden

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

51 files changed

+580
-434
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: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ and should return a middleware array.
9292
For more details on how the `middleware` parameter works and the list of middleware that are added by default, see the
9393
[`getDefaultMiddleware` docs page](./getDefaultMiddleware.mdx).
9494
95+
:::note Tuple
96+
Typescript users are required to use a `Tuple` instance (if not using a `getDefaultMiddleware` result, which is already a `Tuple`), for better inference.
97+
98+
```ts no-transpile
99+
import { configureStore, Tuple } from '@reduxjs/toolkit'
100+
101+
configureStore({
102+
reducer: rootReducer,
103+
middleware: new Tuple(additionalMiddleware, logger),
104+
})
105+
```
106+
107+
Javascript-only users are free to use a plain array if preferred.
108+
109+
:::
110+
95111
### `devTools`
96112
97113
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).
@@ -121,8 +137,7 @@ If defined as an array, these will be passed to [the Redux `compose` function](h
121137
:::tip Dev Tools
122138
This should _not_ include the Redux DevTools Extension `composeWithDevTools`, as this is already handled by `configureStore`.
123139
124-
Example: `enhancers: [offline]` will result in a final setup of `[offline, devToolsExtension]`.
125-
:::
140+
Example: `enhancers: new Tuple(offline)` will result in a final setup of `[offline, devToolsExtension]`.
126141
127142
If not provided, `configureStore` will call `getDefaultEnhancers` and use the array of enhancers it returns (including `applyMiddleware` with specified middleware).
128143
@@ -135,8 +150,6 @@ For more details on how the `enhancer` parameter works and the list of enhancers
135150
136151
:::caution Middleware
137152
138-
In order to use any [middleware](#middleware) specified, you _need_ to use the `getDefaultEnhancers` callback, which will include the built `applyMiddleware` enhancer.
139-
140153
If you provide an array, this `applyMiddleware` enhancer will _not_ be used.
141154
142155
`configureStore` will warn in console if any middleware are provided (or left as default) but not included in the final list of enhancers.
@@ -170,8 +183,23 @@ configureStore({
170183
171184
:::
172185
173-
## Usage
186+
:::note Tuple
187+
Typescript users are required to use a `Tuple` instance (if not using a `getDefaultEnhancer` result, which is already a `Tuple`), for better inference.
188+
189+
import { configureStore, Tuple } from '@reduxjs/toolkit'
190+
191+
configureStore({
192+
reducer: rootReducer,
193+
enhancers: new Tuple(offline),
194+
})
195+
196+
````
174197
198+
Javascript-only users are free to use a plain array if preferred.
199+
200+
:::
201+
202+
## Usage
175203
### Basic Example
176204
177205
```ts
@@ -187,7 +215,7 @@ import rootReducer from './reducers'
187215

188216
const store = configureStore({ reducer: rootReducer })
189217
// The store now has redux-thunk added and the Redux DevTools Extension is turned on
190-
```
218+
````
191219

192220
### Full Example
193221

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)