Skip to content

Commit 40cdc44

Browse files
committed
Merge branch 'master' into combineslices-example
2 parents fd8f9bb + 6037afa commit 40cdc44

File tree

63 files changed

+1607
-495
lines changed

Some content is hidden

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

63 files changed

+1607
-495
lines changed

.codesandbox/ci.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"sandboxes": [
33
"vanilla",
44
"vanilla-ts",
5-
"github/reduxjs/rtk-github-issues-example",
65
"/examples/query/react/basic",
76
"/examples/query/react/advanced",
87
"/examples/action-listener/counter",

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ jobs:
106106
fail-fast: false
107107
matrix:
108108
node: ['20.x']
109-
ts: ['4.7', '4.8', '4.9', '5.0', '5.1', '5.2', '5.3']
109+
ts: ['4.7', '4.8', '4.9', '5.0', '5.1', '5.2', '5.3', '5.4']
110110
steps:
111111
- name: Checkout repo
112112
uses: actions/checkout@v4

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-with-typescript.mdx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ When using `fetchBaseQuery`, the `error` property returned from a hook will have
593593
If an error is present, you can access error properties after narrowing the type to either `FetchBaseQueryError` or `SerializedError`.
594594

595595
```tsx no-transpile
596-
import { api } from './services/api'
596+
import { usePostsQuery } from './services/api'
597597

598598
function PostDetail() {
599599
const { data, error, isLoading } = usePostsQuery()
@@ -613,10 +613,9 @@ function PostDetail() {
613613
<div>{errMsg}</div>
614614
</div>
615615
)
616-
} else {
617-
// you can access all properties of `SerializedError` here
618-
return <div>{error.message}</div>
619-
}
616+
}
617+
// you can access all properties of `SerializedError` here
618+
return <div>{error.message}</div>
620619
}
621620

622621
if (data) {

docs/rtk-query/usage/code-generation.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ export type EndpointMatcherFunction = (
119119
#### Filtering endpoints
120120
121121
If you only want to include a few endpoints, you can use the `filterEndpoints` config option to filter your endpoints.
122+
Note that endpoints are transformed to camel case. For example, `login_user` will become `loginUser`.
123+
`filterEndpoints` will be checked against this camel case version of the endpoint.
122124
123125
```ts no-transpile title="openapi-config.ts"
124126
const filteredConfig: ConfigFile = {

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/tutorials/quick-start.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ npm install @reduxjs/toolkit react-redux
4949

5050
Create a file named `src/app/store.js`. Import the `configureStore` API from Redux Toolkit. We'll start by creating an empty Redux store, and exporting it:
5151

52-
```ts title="app/store.js"
52+
```ts title="app/store.ts"
5353
import { configureStore } from '@reduxjs/toolkit'
5454

5555
export const store = configureStore({
@@ -303,4 +303,4 @@ Here's the complete counter application as a running CodeSandbox:
303303

304304
## What's Next?
305305

306-
We recommend going through [**the "Redux Essentials" and "Redux Fundamentals" tutorials in the Redux core docs**](https://redux.js.org/tutorials/index), which will give you a complete understanding of how Redux works, what Redux Toolkit does, and how to use it correctly.
306+
We recommend going through [**the "Redux Essentials" and "Redux Fundamentals" tutorials in the Redux core docs**](https://redux.js.org/tutorials/index), which will give you a complete understanding of how Redux works, what Redux Toolkit does, and how to use it correctly.

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

0 commit comments

Comments
 (0)