Skip to content

Commit 0423bb0

Browse files
committed
Merge branch 'master' of https://github.com/reduxjs/redux-toolkit into configs
2 parents aec7081 + 32b0155 commit 0423bb0

File tree

8 files changed

+58
-17
lines changed

8 files changed

+58
-17
lines changed

docs/api/createSlice.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ reducers: (create) => {
353353

354354
### `extraReducers`
355355

356-
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.
356+
Conceptually, each slice reducer "owns" its slice of state. There's also a natural correspondence between the update logic defined inside `reducers`, and the action types that are generated based on those.
357357

358358
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.
359359

docs/rtk-query/api/created-api/hooks.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ selectFromResult: () => ({})
370370
- `trigger`: A function that triggers an update to the data based on the provided argument. The trigger function returns a promise with the properties shown above that may be used to handle the behavior of the promise
371371
- `mutationState`: A query status object containing the current loading state and metadata about the request, or the values returned by the `selectFromResult` option where applicable.
372372
Additionally, this object will contain
373-
- a `reset` method to reset the hook back to it's original state and remove the current result from the cache
373+
- a `reset` method to reset the hook back to its original state and remove the current result from the cache
374374
- an `originalArgs` property that contains the argument passed to the last call of the `trigger` function.
375375
376376
#### Description

docs/rtk-query/api/fetchBaseQuery.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ export const api = createApi({
336336
query: () => ({
337337
url: `users`,
338338
// Example: we know the users endpoint is _really fast_ because it's always cached.
339-
// We can assume if its over > 1000ms, something is wrong and we should abort the request.
339+
// We can assume if it's over > 1000ms, something is wrong and we should abort the request.
340340
timeout: 1000,
341341
}),
342342
}),

docs/rtk-query/usage/mutations.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Below are some of the most frequently used properties on the "mutation result" o
118118
- `isLoading` - When true, indicates that the mutation has been fired and is awaiting a response.
119119
- `isSuccess` - When true, indicates that the last mutation fired has data from a successful request.
120120
- `isError` - When true, indicates that the last mutation fired resulted in an error state.
121-
- `reset` - A method to reset the hook back to it's original state and remove the current result from the cache
121+
- `reset` - A method to reset the hook back to its original state and remove the current result from the cache
122122

123123
:::note
124124

docs/usage/usage-with-typescript.md

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -724,20 +724,46 @@ Import and use that pre-typed `createAppAsyncThunk` instead of the original, and
724724

725725
## `createEntityAdapter`
726726

727-
Typing `createEntityAdapter` only requires you to specify the entity type as the single generic argument.
727+
Usage of `createEntityAdapter` with Typescript varies based on whether your entities are normalized by an `id` property, or whether a custom `selectId` is needed.
728728

729-
The example from the `createEntityAdapter` documentation would look like this in TypeScript:
729+
If your entities are normalized by an `id` property, `createEntityAdapter` only requires you to specify the entity type as the single generic argument. For example:
730730

731731
```ts
732732
interface Book {
733-
bookId: number
733+
id: number
734734
title: string
735-
// ...
736735
}
737736

737+
// no selectId needed here, as the entity has an `id` property we can default to
738738
// highlight-next-line
739739
const booksAdapter = createEntityAdapter<Book>({
740-
selectId: (book) => book.bookId,
740+
sortComparer: (a, b) => a.title.localeCompare(b.title),
741+
})
742+
743+
const booksSlice = createSlice({
744+
name: 'books',
745+
initialState: booksAdapter.getInitialState(),
746+
reducers: {
747+
bookAdded: booksAdapter.addOne,
748+
booksReceived(state, action: PayloadAction<{ books: Book[] }>) {
749+
booksAdapter.setAll(state, action.payload.books)
750+
},
751+
},
752+
})
753+
```
754+
755+
On the other hand, if the entity needs to be normalized by a different property, we instead recommend passing a custom `selectId` function and annotating there. This allows proper inference of the ID's type, instead of having to provide it manually.
756+
757+
```ts
758+
interface Book {
759+
bookId: number
760+
title: string
761+
// ...
762+
}
763+
764+
const booksAdapter = createEntityAdapter({
765+
// highlight-next-line
766+
selectId: (book: Book) => book.bookId,
741767
sortComparer: (a, b) => a.title.localeCompare(b.title),
742768
})
743769

packages/toolkit/src/query/core/buildThunks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
515515
arg.forceRefetch ?? (arg.subscribe && baseFetchOnMountOrArgChange)
516516

517517
if (refetchVal) {
518-
// Return if its true or compare the dates because it must be a number
518+
// Return if it's true or compare the dates because it must be a number
519519
return (
520520
refetchVal === true ||
521521
(Number(new Date()) - Number(fulfilledVal)) / 1000 >= refetchVal

packages/toolkit/src/query/endpointDefinitions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ interface EndpointDefinitionWithQueryFn<
136136
* if (randomVal < 0.9) {
137137
* return { data: 'tails' }
138138
* }
139-
* return { error: { status: 500, statusText: 'Internal Server Error', data: "Coin landed on it's edge!" } }
139+
* return { error: { status: 500, statusText: 'Internal Server Error', data: "Coin landed on its edge!" } }
140140
* }
141141
* // highlight-end
142142
* })

packages/toolkit/src/query/react/buildHooks.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,27 @@ import { useStableQueryArgs } from './useSerializedStableValue'
5353
import { useShallowStableValue } from './useShallowStableValue'
5454

5555
// Copy-pasted from React-Redux
56+
const canUseDOM = () =>
57+
!!(
58+
typeof window !== 'undefined' &&
59+
typeof window.document !== 'undefined' &&
60+
typeof window.document.createElement !== 'undefined'
61+
)
62+
63+
const isDOM = /* @__PURE__ */ canUseDOM()
64+
65+
// Under React Native, we know that we always want to use useLayoutEffect
66+
67+
const isRunningInReactNative = () =>
68+
typeof navigator !== 'undefined' && navigator.product === 'ReactNative'
69+
70+
const isReactNative = /* @__PURE__ */ isRunningInReactNative()
71+
72+
const getUseIsomorphicLayoutEffect = () =>
73+
isDOM || isReactNative ? useLayoutEffect : useEffect
74+
5675
export const useIsomorphicLayoutEffect =
57-
typeof window !== 'undefined' &&
58-
!!window.document &&
59-
!!window.document.createElement
60-
? useLayoutEffect
61-
: useEffect
76+
/* @__PURE__ */ getUseIsomorphicLayoutEffect()
6277

6378
export interface QueryHooks<
6479
Definition extends QueryDefinition<any, any, any, any, any>,
@@ -521,7 +536,7 @@ export type UseMutationStateResult<
521536
> = TSHelpersNoInfer<R> & {
522537
originalArgs?: QueryArgFrom<D>
523538
/**
524-
* Resets the hook state to it's initial `uninitialized` state.
539+
* Resets the hook state to its initial `uninitialized` state.
525540
* This will also remove the last result from the cache.
526541
*/
527542
reset: () => void

0 commit comments

Comments
 (0)