Skip to content

Commit baa8465

Browse files
committed
Merge branch 'feature/v1.6-integration' into chore/readonly-array-params
2 parents 2905b9f + 07b0a7c commit baa8465

File tree

285 files changed

+250278
-8262
lines changed

Some content is hidden

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

285 files changed

+250278
-8262
lines changed

docs/api/configureStore.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ sidebar_label: configureStore
55
hide_title: true
66
---
77

8+
 
9+
810
# `configureStore`
911

1012
A friendly abstraction over the standard Redux `createStore` function that adds good defaults

docs/api/createAction.mdx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ sidebar_label: createAction
55
hide_title: true
66
---
77

8+
 
9+
810
# `createAction`
911

1012
A helper function for defining a Redux [action](https://redux.js.org/basics/actions) type and creator.
@@ -21,7 +23,7 @@ const INCREMENT = 'counter/increment'
2123
function increment(amount: number) {
2224
return {
2325
type: INCREMENT,
24-
payload: amount
26+
payload: amount,
2527
}
2628
}
2729

@@ -63,8 +65,8 @@ const addTodo = createAction('todos/add', function prepare(text: string) {
6365
payload: {
6466
text,
6567
id: nanoid(),
66-
createdAt: new Date().toISOString()
67-
}
68+
createdAt: new Date().toISOString(),
69+
},
6870
}
6971
})
7072

@@ -95,7 +97,7 @@ import { createAction, createReducer } from '@reduxjs/toolkit'
9597
const increment = createAction<number>('counter/increment')
9698
const decrement = createAction<number>('counter/decrement')
9799

98-
const counterReducer = createReducer(0, builder => {
100+
const counterReducer = createReducer(0, (builder) => {
99101
builder.addCase(increment, (state, action) => state + action.payload)
100102
builder.addCase(decrement, (state, action) => state - action.payload)
101103
})
@@ -133,7 +135,7 @@ const counterReducer = createReducer(0, {
133135
[increment]: (state, action) => state + action.payload,
134136

135137
// You would need to use the action type explicitly instead.
136-
[INCREMENT]: (state, action) => state + action.payload
138+
[INCREMENT]: (state, action) => state + action.payload,
137139
})
138140
```
139141
@@ -178,7 +180,7 @@ const increment = createAction<number>('INCREMENT')
178180
export const epic = (actions$: Observable<Action>) =>
179181
actions$.pipe(
180182
filter(increment.match),
181-
map(action => {
183+
map((action) => {
182184
// action.payload can be safely used as number here (and will also be correctly inferred by TypeScript)
183185
// ...
184186
})

docs/api/createAsyncThunk.mdx

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ sidebar_label: createAsyncThunk
55
hide_title: true
66
---
77

8+
&nbsp;
9+
810
# `createAsyncThunk`
911

1012
## Overview
@@ -42,8 +44,8 @@ const usersSlice = createSlice({
4244
[fetchUserById.fulfilled]: (state, action) => {
4345
// Add user to the state array
4446
state.entities.push(action.payload)
45-
}
46-
}
47+
},
48+
},
4749
})
4850

4951
// Later, dispatch the thunk as needed in the app
@@ -79,17 +81,20 @@ The `payloadCreator` function will be called with two arguments:
7981
- `extra`: the "extra argument" given to the thunk middleware on setup, if available
8082
- `requestId`: a unique string ID value that was automatically generated to identify this request sequence
8183
- `signal`: an [`AbortController.signal` object](https://developer.mozilla.org/en-US/docs/Web/API/AbortController/signal) that may be used to see if another part of the app logic has marked this request as needing cancelation.
82-
- `rejectWithValue`: rejectWithValue is a utility function that you can `return` in your action creator to return a rejected response with a defined payload. It will pass whatever value you give it and return it in the payload of the rejected action.
84+
- `rejectWithValue(value, [meta])`: rejectWithValue is a utility function that you can `return` (or `throw`) in your action creator to return a rejected response with a defined payload and meta. It will pass whatever value you give it and return it in the payload of the rejected action. If you also pass in a `meta`, it will be merged with the existing `rejectedAction.meta`.
85+
- `fulfillWithValue(value, meta)`: fulfillWithValue is a utility function that you can `return` in your action creator to `fulfill` with a value while having the ability of adding to `fulfilledAction.meta`.
8386

8487
The logic in the `payloadCreator` function may use any of these values as needed to calculate the result.
8588

8689
### Options
8790

8891
An object with the following optional fields:
8992

90-
- `condition`: a callback that can be used to skip execution of the payload creator and all action dispatches, if desired. See [Canceling Before Execution](#canceling-before-execution) for a complete description.
93+
- `condition(arg, { getState, extra } ): boolean`: a callback that can be used to skip execution of the payload creator and all action dispatches, if desired. See [Canceling Before Execution](#canceling-before-execution) for a complete description.
9194
- `dispatchConditionRejection`: if `condition()` returns `false`, the default behavior is that no actions will be dispatched at all. If you still want a "rejected" action to be dispatched when the thunk was canceled, set this flag to `true`.
92-
- `idGenerator`: a function to use when generating the `requestId` for the request sequence. Defaults to use [nanoid](./otherExports.mdx/#nanoid).
95+
- `idGenerator(): string`: a function to use when generating the `requestId` for the request sequence. Defaults to use [nanoid](./otherExports.mdx/#nanoid).
96+
- `serializeError(error: unknown) => any` to replace the internal `miniSerializeError` method with your own serialization logic.
97+
- `getPendingMeta({ arg, requestId }, { getState, extra }): any`: a function to create an object that will be merged into the `pendingAction.meta` field.
9398

9499
## Return Value
95100

@@ -193,10 +198,10 @@ To handle these actions in your reducers, reference the action creators in `crea
193198
194199
```js {2,6,14,23}
195200
const reducer1 = createReducer(initialState, {
196-
[fetchUserById.fulfilled]: (state, action) => {}
201+
[fetchUserById.fulfilled]: (state, action) => {},
197202
})
198203

199-
const reducer2 = createReducer(initialState, builder => {
204+
const reducer2 = createReducer(initialState, (builder) => {
200205
builder.addCase(fetchUserById.fulfilled, (state, action) => {})
201206
})
202207

@@ -205,17 +210,17 @@ const reducer3 = createSlice({
205210
initialState,
206211
reducers: {},
207212
extraReducers: {
208-
[fetchUserById.fulfilled]: (state, action) => {}
209-
}
213+
[fetchUserById.fulfilled]: (state, action) => {},
214+
},
210215
})
211216

212217
const reducer4 = createSlice({
213218
name: 'users',
214219
initialState,
215220
reducers: {},
216-
extraReducers: builder => {
221+
extraReducers: (builder) => {
217222
builder.addCase(fetchUserById.fulfilled, (state, action) => {})
218-
}
223+
},
219224
})
220225
```
221226

@@ -244,8 +249,8 @@ import { unwrapResult } from '@reduxjs/toolkit'
244249
const onClick = () => {
245250
dispatch(fetchUserById(userId))
246251
.then(unwrapResult)
247-
.then(originalPromiseResult => {})
248-
.catch(rejectedValueOrSerializedError => {})
252+
.then((originalPromiseResult) => {})
253+
.catch((rejectedValueOrSerializedError) => {})
249254
}
250255
```
251256

@@ -310,7 +315,7 @@ const fetchUserById = createAsyncThunk(
310315
// Already fetched or in progress, don't need to re-fetch
311316
return false
312317
}
313-
}
318+
},
314319
}
315320
)
316321
```
@@ -372,7 +377,7 @@ const fetchUserById = createAsyncThunk(
372377
'users/fetchById',
373378
async (userId: string, thunkAPI) => {
374379
const response = await fetch(`https://reqres.in/api/users/${userId}`, {
375-
signal: thunkAPI.signal
380+
signal: thunkAPI.signal,
376381
})
377382
return await response.json()
378383
}
@@ -426,7 +431,7 @@ const fetchUserById = createAsyncThunk(
426431
source.cancel()
427432
})
428433
const response = await axios.get(`https://reqres.in/api/users/${userId}`, {
429-
cancelToken: source.token
434+
cancelToken: source.token,
430435
})
431436
return response.data
432437
}
@@ -459,7 +464,7 @@ const usersSlice = createSlice({
459464
entities: [],
460465
loading: 'idle',
461466
currentRequestId: undefined,
462-
error: null
467+
error: null,
463468
},
464469
reducers: {},
465470
extraReducers: {
@@ -484,15 +489,15 @@ const usersSlice = createSlice({
484489
state.error = action.error
485490
state.currentRequestId = undefined
486491
}
487-
}
488-
}
492+
},
493+
},
489494
})
490495

491496
const UsersComponent = () => {
492-
const { users, loading, error } = useSelector(state => state.users)
497+
const { users, loading, error } = useSelector((state) => state.users)
493498
const dispatch = useDispatch()
494499

495-
const fetchOneUser = async userId => {
500+
const fetchOneUser = async (userId) => {
496501
try {
497502
const resultAction = await dispatch(fetchUserById(userId))
498503
const user = unwrapResult(resultAction)
@@ -577,14 +582,14 @@ interface UsersState {
577582

578583
const initialState = {
579584
entities: {},
580-
error: null
585+
error: null,
581586
} as UsersState
582587

583588
const usersSlice = createSlice({
584589
name: 'users',
585590
initialState,
586591
reducers: {},
587-
extraReducers: builder => {
592+
extraReducers: (builder) => {
588593
// The `builder` callback form is used here because it provides correctly typed reducers from the action creators
589594
builder.addCase(updateUser.fulfilled, (state, { payload }) => {
590595
state.entities[payload.id] = payload
@@ -597,7 +602,7 @@ const usersSlice = createSlice({
597602
state.error = action.error.message
598603
}
599604
})
600-
}
605+
},
601606
})
602607

603608
export default usersSlice.reducer

docs/api/createEntityAdapter.mdx

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ sidebar_label: createEntityAdapter
55
hide_title: true
66
---
77

8+
&nbsp;
9+
810
# `createEntityAdapter`
911

1012
## Overview
@@ -39,16 +41,16 @@ Sample usage:
3941
import {
4042
createEntityAdapter,
4143
createSlice,
42-
configureStore
44+
configureStore,
4345
} from '@reduxjs/toolkit'
4446

4547
type Book = { bookId: string; title: string }
4648

4749
const booksAdapter = createEntityAdapter<Book>({
4850
// Assume IDs are stored in a field other than `book.id`
49-
selectId: book => book.bookId,
51+
selectId: (book) => book.bookId,
5052
// Keep the "all IDs" array sorted based on book titles
51-
sortComparer: (a, b) => a.title.localeCompare(b.title)
53+
sortComparer: (a, b) => a.title.localeCompare(b.title),
5254
})
5355

5456
const booksSlice = createSlice({
@@ -61,14 +63,14 @@ const booksSlice = createSlice({
6163
booksReceived(state, action) {
6264
// Or, call them as "mutating" helpers in a case reducer
6365
booksAdapter.setAll(state, action.payload.books)
64-
}
65-
}
66+
},
67+
},
6668
})
6769

6870
const store = configureStore({
6971
reducer: {
70-
books: booksSlice.reducer
71-
}
72+
books: booksSlice.reducer,
73+
},
7274
})
7375

7476
type RootState = ReturnType<typeof store.getState>
@@ -78,7 +80,7 @@ console.log(store.getState().books)
7880

7981
// Can create a set of memoized selectors based on the location of this entity state
8082
const booksSelectors = booksAdapter.getSelectors<RootState>(
81-
state => state.books
83+
(state) => state.books
8284
)
8385

8486
// And then use the selectors to retrieve values
@@ -243,14 +245,14 @@ It accepts an optional object as an argument. The fields in that object will be
243245
const booksSlice = createSlice({
244246
name: 'books',
245247
initialState: booksAdapter.getInitialState({
246-
loading: 'idle'
248+
loading: 'idle',
247249
}),
248250
reducers: {
249251
booksLoadingStarted(state, action) {
250252
// Can update the additional state field
251253
state.loading = 'pending'
252-
}
253-
}
254+
},
255+
},
254256
})
255257
```
256258

@@ -276,12 +278,12 @@ For example, the entity state for a `Book` type might be kept in the Redux state
276278
```js
277279
const store = configureStore({
278280
reducer: {
279-
books: booksReducer
280-
}
281+
books: booksReducer,
282+
},
281283
})
282284

283285
const simpleSelectors = booksAdapter.getSelectors()
284-
const globalizedSelectors = booksAdapter.getSelectors(state => state.books)
286+
const globalizedSelectors = booksAdapter.getSelectors((state) => state.books)
285287

286288
// Need to manually pass the correct entity state object in to this selector
287289
const bookIds = simpleSelectors.selectIds(store.getState().books)
@@ -306,19 +308,19 @@ Exercising several of the CRUD methods and selectors:
306308
import {
307309
createEntityAdapter,
308310
createSlice,
309-
configureStore
311+
configureStore,
310312
} from '@reduxjs/toolkit'
311313

312314
// Since we don't provide `selectId`, it defaults to assuming `entity.id` is the right field
313315
const booksAdapter = createEntityAdapter({
314316
// Keep the "all IDs" array sorted based on book titles
315-
sortComparer: (a, b) => a.title.localeCompare(b.title)
317+
sortComparer: (a, b) => a.title.localeCompare(b.title),
316318
})
317319

318320
const booksSlice = createSlice({
319321
name: 'books',
320322
initialState: booksAdapter.getInitialState({
321-
loading: 'idle'
323+
loading: 'idle',
322324
}),
323325
reducers: {
324326
// Can pass adapter functions directly as case reducers. Because we're passing this
@@ -336,28 +338,28 @@ const booksSlice = createSlice({
336338
state.loading = 'idle'
337339
}
338340
},
339-
bookUpdated: booksAdapter.updateOne
340-
}
341+
bookUpdated: booksAdapter.updateOne,
342+
},
341343
})
342344

343345
const {
344346
bookAdded,
345347
booksLoading,
346348
booksReceived,
347-
bookUpdated
349+
bookUpdated,
348350
} = booksSlice.actions
349351

350352
const store = configureStore({
351353
reducer: {
352-
books: booksSlice.reducer
353-
}
354+
books: booksSlice.reducer,
355+
},
354356
})
355357

356358
// Check the initial state:
357359
console.log(store.getState().books)
358360
// {ids: [], entities: {}, loading: 'idle' }
359361

360-
const booksSelectors = booksAdapter.getSelectors(state => state.books)
362+
const booksSelectors = booksAdapter.getSelectors((state) => state.books)
361363

362364
store.dispatch(bookAdded({ id: 'a', title: 'First' }))
363365
console.log(store.getState().books)
@@ -371,7 +373,7 @@ console.log(store.getState().books)
371373
store.dispatch(
372374
booksReceived([
373375
{ id: 'b', title: 'Book 3' },
374-
{ id: 'c', title: 'Book 2' }
376+
{ id: 'c', title: 'Book 2' },
375377
])
376378
)
377379

0 commit comments

Comments
 (0)