Skip to content

Commit 6e54666

Browse files
committed
Merge branch 'v2.0-integration' into cdm-tweaks
2 parents e15797a + 289ae45 commit 6e54666

File tree

103 files changed

+2195
-3749
lines changed

Some content is hidden

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

103 files changed

+2195
-3749
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
github: [phryneas, markerikson]
1+
github: [phryneas, markerikson, EskiMojo14]

.github/workflows/tests.yml

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ jobs:
105105
fail-fast: false
106106
matrix:
107107
node: ['16.x']
108-
ts: ['4.7', '4.8', '4.9', '5.0']
108+
ts: ['4.7', '4.8', '4.9', '5.0', '5.1', '5.2']
109109
steps:
110110
- name: Checkout repo
111111
uses: actions/checkout@v2
@@ -149,16 +149,7 @@ jobs:
149149
fail-fast: false
150150
matrix:
151151
node: ['16.x']
152-
example:
153-
[
154-
'cra4',
155-
'cra5',
156-
'next',
157-
'vite',
158-
'node-standard',
159-
'node-esm',
160-
'are-the-types-wrong',
161-
]
152+
example: ['cra4', 'cra5', 'next', 'vite', 'node-standard', 'node-esm']
162153
defaults:
163154
run:
164155
working-directory: ./examples/publish-ci/${{ matrix.example }}
@@ -197,9 +188,27 @@ jobs:
197188

198189
- name: Run test step
199190
run: yarn test
200-
if: matrix.example != 'are-the-types-wrong'
201191

202-
- name: Run test step (attw)
203-
# Ignore "FalseCJS" errors in the `attw` job
204-
run: yarn test -n FalseCJS
205-
if: matrix.example == 'are-the-types-wrong'
192+
are-the-types-wrong:
193+
name: Check package config with are-the-types-wrong
194+
195+
needs: [build]
196+
runs-on: ubuntu-latest
197+
strategy:
198+
fail-fast: false
199+
matrix:
200+
node: ['16.x']
201+
steps:
202+
- name: Checkout repo
203+
uses: actions/checkout@v3
204+
205+
- uses: actions/download-artifact@v2
206+
with:
207+
name: package
208+
path: packages/toolkit
209+
210+
- name: show folder
211+
run: ls -l .
212+
213+
- name: Run are-the-types-wrong
214+
run: npx @arethetypeswrong/cli ./package.tgz --format table --ignore-rules false-cjs

docs/api/actionCreatorMiddleware.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@ const actionCreatorMiddleware = createActionCreatorInvariantMiddleware({
6363

6464
const store = configureStore({
6565
reducer,
66-
middleware: new Tuple(actionCreatorMiddleware),
66+
middleware: () => new Tuple(actionCreatorMiddleware),
6767
})
6868
```

docs/api/configureStore.mdx

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,28 @@ hide_title: true
99

1010
# `configureStore`
1111

12-
A friendly abstraction over the standard Redux `createStore` function that adds good defaults
13-
to the store setup for a better development experience.
12+
The standard method for creating a Redux store. It uses the low-level Redux core `createStore` method internally, but wraps that to provide good defaults to the store setup for a better development experience.
13+
14+
## Purpose and Behavior
15+
16+
A standard Redux store setup typically requires multiple pieces of configuration:
17+
18+
- Combining the slice reducers into the root reducer
19+
- Creating the middleware enhancer, usually with the thunk middleware or other side effects middleware, as well as middleware that might be used for development checks
20+
- Adding the Redux DevTools enhancer, and composing the enhancers together
21+
- Calling `createStore`
22+
23+
Legacy Redux usage patterns typically required several dozen lines of copy-pasted boilerplate to achieve this.
24+
25+
Redux Toolkit's `configureStore` simplifies that setup process, by doing all that work for you. One call to `configureStore` will:
26+
27+
- Call `combineReducers` to combine your slices reducers into the root reducer function
28+
- Add the thunk middleware and called `applyMiddleware`
29+
- In development, automatically add more middleware to check for common mistakes like accidentally mutating the state
30+
- Automatically set up the Redux DevTools Extension connection
31+
- Call `createStore` to create a Redux store using that root reducer and those configuration options
32+
33+
`configureStore` also offers an improved API and usage patterns compared to the original `createStore` by accepting named fields for `reducer`, `preloadedState`, `middleware`, `enhancers`, and `devtools`, as well as much better TS type inference.
1434

1535
## Parameters
1636

@@ -80,18 +100,15 @@ If it is an object of slice reducers, like `{users : usersReducer, posts : posts
80100

81101
### `middleware`
82102

83-
An optional array of Redux middleware functions, or a callback to customise the array of middleware.
103+
A callback which will receive `getDefaultMiddleware` as its argument,
104+
and should return a middleware array.
84105

85-
If this option is provided, it should contain all the middleware functions you
106+
If this option is provided, it should return all the middleware functions you
86107
want added to the store. `configureStore` will automatically pass those to `applyMiddleware`.
87108

88109
If not provided, `configureStore` will call `getDefaultMiddleware` and use the
89110
array of middleware functions it returns.
90111

91-
Where you wish to add onto or customize the default middleware,
92-
you may pass a callback function that will receive `getDefaultMiddleware` as its argument,
93-
and should return a middleware array.
94-
95112
For more details on how the `middleware` parameter works and the list of middleware that are added by default, see the
96113
[`getDefaultMiddleware` docs page](./getDefaultMiddleware.mdx).
97114

@@ -103,7 +120,7 @@ import { configureStore, Tuple } from '@reduxjs/toolkit'
103120
104121
configureStore({
105122
reducer: rootReducer,
106-
middleware: new Tuple(additionalMiddleware, logger),
123+
middleware: () => new Tuple(additionalMiddleware, logger),
107124
})
108125
```
109126

@@ -158,11 +175,6 @@ If you provide an array, this `applyMiddleware` enhancer will _not_ be used.
158175
`configureStore` will warn in console if any middleware are provided (or left as default) but not included in the final list of enhancers.
159176
160177
```ts no-transpile
161-
// warns - middleware left as default but not included in final enhancers
162-
configureStore({
163-
reducer,
164-
enhancers: [offline(offlineConfig)],
165-
})
166178
// warns - middleware customised but not included in final enhancers
167179
configureStore({
168180
reducer,
@@ -179,8 +191,8 @@ configureStore({
179191
// also allowed
180192
configureStore({
181193
reducer,
182-
middleware: [],
183-
enhancers: [offline(offlineConfig)],
194+
middleware: () => [],
195+
enhancers: () => [offline(offlineConfig)],
184196
})
185197
```
186198

@@ -189,20 +201,22 @@ configureStore({
189201
:::note Tuple
190202
Typescript users are required to use a `Tuple` instance (if not using a `getDefaultEnhancer` result, which is already a `Tuple`), for better inference.
191203

204+
```
192205
import { configureStore, Tuple } from '@reduxjs/toolkit'
193206

194207
configureStore({
195208
reducer: rootReducer,
196-
enhancers: new Tuple(offline),
209+
enhancers: () => new Tuple(offline),
197210
})
198211

199-
````
212+
```
200213
201214
Javascript-only users are free to use a plain array if preferred.
202215
203216
:::
204217
205218
## Usage
219+
206220
### Basic Example
207221
208222
```ts
@@ -218,7 +232,7 @@ import rootReducer from './reducers'
218232

219233
const store = configureStore({ reducer: rootReducer })
220234
// The store now has redux-thunk added and the Redux DevTools Extension is turned on
221-
````
235+
```
222236
223237
### Full Example
224238

docs/api/createSlice.mdx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ and automatically generates action creators and action types that correspond to
1515
This API is the standard approach for writing Redux logic.
1616

1717
Internally, it uses [`createAction`](./createAction.mdx) and [`createReducer`](./createReducer.mdx), so
18-
you may also use [Immer](https://immerjs.github.io/immer/) to write "mutating" immutable updates:
18+
you may also use [Immer](../usage/immer-reducers.md) to write "mutating" immutable updates:
1919

2020
```ts
2121
import { createSlice } from '@reduxjs/toolkit'
@@ -142,7 +142,6 @@ The main benefit of this is that you can create [async thunks](./createAsyncThun
142142

143143
```ts title="Creator callback for reducers"
144144
import { createSlice, nanoid } from '@reduxjs/toolkit'
145-
import type { PayloadAction } from '@reduxjs/toolkit'
146145
147146
interface Item {
148147
id: string
@@ -161,7 +160,7 @@ const todosSlice = createSlice({
161160
todos: [],
162161
} as TodoState,
163162
reducers: (create) => ({
164-
deleteTodo: create.reducer((state, action: PayloadAction<number>) => {
163+
deleteTodo: create.reducer<number>((state, action) => {
165164
state.todos.splice(action.payload, 1)
166165
}),
167166
addTodo: create.preparedReducer(
@@ -209,7 +208,7 @@ A standard slice case reducer.
209208
- **reducer** The slice case reducer to use.
210209

211210
```ts no-transpile
212-
create.reducer((state, action: PayloadAction<Todo>) => {
211+
create.reducer<Todo>((state, action) => {
213212
state.todos.push(action.payload)
214213
})
215214
```
@@ -322,16 +321,17 @@ reducers: (create) => {
322321

323322
### `extraReducers`
324323

325-
One of the key concepts of Redux is that each slice reducer "owns" its slice of state, and that many slice reducers
326-
can independently respond to the same action type. `extraReducers` allows `createSlice` to respond to other action types
327-
besides the types it has generated.
324+
Conceptually, each slice reducer "owns" its slice of state. There's also a natural correspondance between the update logic defined inside `reducers`, and the action types that are generated based on those.
325+
326+
However, there are many times that a Redux slice may also need to update its own state in response to action types that were defined elsewhere in the application (such as clearing many different kinds of data when a "user logged out" action is dispatched). This can include action types defined by another `createSlice` call, actions generated by a `createAsyncThunk`, RTK Query endpoint matchers, or any other action. In addition, one of the key concepts of Redux is that many slice reducers can independently respond to the same action type.
327+
328+
**`extraReducers` allows `createSlice` to respond and update its own state in response to other action types besides the types it has generated.**
328329

329-
As case reducers specified with `extraReducers` are meant to reference "external" actions, they will not have actions generated in `slice.actions`.
330+
As with the `reducers` field, each case reducer in `extraReducers` is [wrapped in Immer and may use "mutating" syntax to safely update the state inside](../usage/immer-reducers.md).
330331

331-
As with `reducers`, these case reducers will also be passed to `createReducer` and may "mutate" their state safely.
332+
However, unlike the `reducers` field, each individual case reducer inside of `extraReducers` will _not_ generate a new action type or action creator.
332333

333-
If two fields from `reducers` and `extraReducers` happen to end up with the same action type string,
334-
the function from `reducers` will be used to handle that action type.
334+
If two fields from `reducers` and `extraReducers` happen to end up with the same action type string, the function from `reducers` will be used to handle that action type.
335335

336336
#### The `extraReducers` "builder callback" notation
337337

@@ -373,6 +373,7 @@ const counterSlice = createSlice({
373373
})
374374
```
375375

376+
<<<<<<< HEAD
376377
This cycle can be fixed by providing an explicit return type for the selector:
377378

378379
```ts no-transpile
@@ -418,6 +419,10 @@ const counterSlice = createSlice({
418419

419420
:::
420421

422+
=======
423+
424+
> > > > > > > master
425+
421426
## Return Value
422427

423428
`createSlice` will return an object that looks like:

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: new Tuple(thunk, logger),
31+
middleware: () => new Tuple(thunk, logger),
3232
})
3333

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

docs/api/immutabilityMiddleware.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ const immutableInvariantMiddleware = createImmutableStateInvariantMiddleware({
8686
const store = configureStore({
8787
reducer: exampleSliceReducer,
8888
// Note that this will replace all default middleware
89-
middleware: new Tuple(immutableInvariantMiddleware),
89+
middleware: () => new Tuple(immutableInvariantMiddleware),
9090
})
9191
```
9292

docs/api/matching-utilities.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ As of Redux 5.0, action types are _required_ to be a string.
4242
A higher-order function that accepts one or more of:
4343

4444
- `redux-toolkit` action creator functions such as the ones produced by:
45-
- [`createAction`](./createAction)
46-
- [`createSlice`](./createSlice#return-value)
47-
- [`createAsyncThunk`](./createAsyncThunk#promise-lifecycle-actions)
45+
- [`createAction`](./createAction.mdx)
46+
- [`createSlice`](./createSlice.mdx#return-value)
47+
- [`createAsyncThunk`](./createAsyncThunk.mdx#promise-lifecycle-actions)
4848
- type guard functions
4949
- custom action creator functions that have a `.match` property that is a type guard
5050

@@ -56,7 +56,7 @@ Accepts the same inputs as `isAllOf` and will return a type guard function that
5656

5757
## `isAsyncThunkAction`
5858

59-
A higher-order function that returns a type guard function that may be used to check whether an action was created by [`createAsyncThunk`](./createAsyncThunk).
59+
A higher-order function that returns a type guard function that may be used to check whether an action was created by [`createAsyncThunk`](./createAsyncThunk.mdx).
6060

6161
```ts title="isAsyncThunkAction usage"
6262
import { isAsyncThunkAction } from '@reduxjs/toolkit'
@@ -128,7 +128,7 @@ function handleRejectedAction(action: UnknownAction) {
128128

129129
## `isRejectedWithValue`
130130

131-
A higher-order function that returns a type guard function that may be used to check whether an action is a 'rejected' action creator from the `createAsyncThunk` promise lifecycle that was created by [`rejectWithValue`](./createAsyncThunk#handling-thunk-errors).
131+
A higher-order function that returns a type guard function that may be used to check whether an action is a 'rejected' action creator from the `createAsyncThunk` promise lifecycle that was created by [`rejectWithValue`](./createAsyncThunk.mdx#handling-thunk-errors).
132132

133133
```ts title="isRejectedWithValue usage"
134134
import { isRejectedWithValue } from '@reduxjs/toolkit'

docs/api/serializabilityMiddleware.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ const serializableMiddleware = createSerializableStateInvariantMiddleware({
111111

112112
const store = configureStore({
113113
reducer,
114-
middleware: new Tuple(serializableMiddleware),
114+
middleware: () => new Tuple(serializableMiddleware),
115115
})
116116
```
117117

docs/rtk-query/api/createApi.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ See also [Invalidating cache data](../usage/automated-refetching.mdx#invalidatin
508508

509509
_(optional, only for query endpoints)_
510510

511-
Overrides the api-wide definition of `keepUnusedDataFor` for this endpoint only.a
511+
Overrides the api-wide definition of `keepUnusedDataFor` for this endpoint only.
512512

513513
[summary](docblock://query/createApi.ts?token=CreateApiOptions.keepUnusedDataFor)
514514

0 commit comments

Comments
 (0)