Skip to content

Commit de1f721

Browse files
committed
Merge branch 'master' into create-slice-creators
2 parents 3d2ecf6 + 9577c69 commit de1f721

File tree

68 files changed

+430
-185
lines changed

Some content is hidden

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

68 files changed

+430
-185
lines changed

docs/api/createEntityAdapter.mdx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,39 @@ const booksSlice = createSlice({
274274
})
275275
```
276276

277+
You can also pass in an array of entities or a `Record<EntityId, T>` object to pre-populate the initial state with some entities:
278+
279+
```js
280+
const booksSlice = createSlice({
281+
name: 'books',
282+
initialState: booksAdapter.getInitialState(
283+
{
284+
loading: 'idle',
285+
},
286+
[
287+
{ id: 'a', title: 'First' },
288+
{ id: 'b', title: 'Second' },
289+
],
290+
),
291+
reducers: {},
292+
})
293+
```
294+
295+
This is equivalent to calling:
296+
297+
```js
298+
const initialState = booksAdapter.getInitialState({
299+
loading: 'idle',
300+
})
301+
302+
const prePopulatedState = booksAdapter.setAll(initialState, [
303+
{ id: 'a', title: 'First' },
304+
{ id: 'b', title: 'Second' },
305+
])
306+
```
307+
308+
The first parameter can be `undefined` if no additional properties are needed.
309+
277310
### Selector Functions
278311

279312
The entity adapter will contain a `getSelectors()` function that returns a set of selectors that know how to read the contents of an entity state object:

docs/api/createSelector.mdx

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ hide_title: true
99

1010
# `createSelector`
1111

12+
## Overview
13+
1214
The `createSelector` utility from the [Reselect library](https://github.com/reduxjs/reselect), re-exported for ease of use.
1315

1416
For more details on using `createSelector`, see:
@@ -24,7 +26,7 @@ allowed using string keypaths as input selectors. This was removed, as it ultima
2426
the string keypaths made static typing for selectors difficult.
2527
:::
2628

27-
# `createDraftSafeSelector`
29+
## `createDraftSafeSelector`
2830

2931
In general, we recommend against using selectors inside of reducers:
3032

@@ -86,3 +88,52 @@ const draftSafeSelector = createWeakMapDraftSafeSelector(
8688
```
8789

8890
:::
91+
92+
### Defining a Pre-Typed `createDraftSelector`
93+
94+
As of RTK 2.1, you can define a "pre-typed" version of `createDraftSafeSelector` that can have the type for `state` built in. This lets you set up those types once, so you don't have to repeat them each time you call `createDraftSafeSelector`.
95+
96+
```ts no-transpile
97+
const createTypedDraftSafeSelector =
98+
createDraftSafeSelector.withTypes<RootState>()
99+
```
100+
101+
Import and use the pre-typed `createTypedDraftSafeSelector` function, and it will automatically know that the `state` argument is of type `RootState`.
102+
103+
:::warning Known Limitations
104+
Currently this approach only works if input selectors are provided as a single array.
105+
106+
If you pass the input selectors as separate inline arguments, the parameter types of the result function will not be inferred. As a workaround you can either
107+
108+
1. Wrap your input selectors in a single array
109+
2. You can annotate the parameter types of the result function:
110+
111+
```ts no-transpile
112+
import { createSelector } from 'reselect'
113+
114+
interface Todo {
115+
id: number
116+
completed: boolean
117+
}
118+
119+
interface Alert {
120+
id: number
121+
read: boolean
122+
}
123+
124+
export interface RootState {
125+
todos: Todo[]
126+
alerts: Alert[]
127+
}
128+
129+
export const createTypedDraftSafeSelector =
130+
createDraftSafeSelector.withTypes<RootState>()
131+
132+
const selectTodoIds = createTypedDraftSafeSelector(
133+
// Type of `state` is set to `RootState`, no need to manually set the type
134+
(state) => state.todos,
135+
// ❌ Known limitation: Parameter types are not inferred in this scenario
136+
// so you will have to manually annotate them.
137+
(todos: Todo[]) => todos.map(({ id }) => id),
138+
)
139+
```

docs/rtk-query/api/created-api/code-splitting.mdx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ const injectEndpoints = (endpointOptions: InjectedEndpointOptions) =>
2525

2626
interface InjectedEndpointOptions {
2727
endpoints: (build: EndpointBuilder) => NewEndpointDefinitions
28-
overrideExisting?: boolean
28+
/**
29+
* Optionally allows endpoints to be overridden if defined by multiple `injectEndpoints` calls.
30+
*
31+
* If set to `true`, will override existing endpoints with the new definition.
32+
* If set to `'throw'`, will throw an error if an endpoint is redefined with a different definition.
33+
* If set to `false` (or unset), will not override existing endpoints with the new definition, and log a warning in development.
34+
*/
35+
overrideExisting?: boolean | 'throw'
2936
}
3037
```
3138

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,7 @@ export const { useExampleQuery } = extendedApi
5656
```
5757

5858
:::tip
59-
If you inject an endpoint that already exists and don't explicitly specify `overrideExisting: true`, the endpoint will not be overridden. In development mode, you will get a warning about this.
59+
If you inject an endpoint that already exists and don't explicitly specify `overrideExisting: true`, the endpoint
60+
will not be overridden. In development mode, you will get a warning about this if `overrideExisting` is set to `false`,
61+
and an error will be throw if set to `'throw'`.
6062
:::

docs/tsconfig.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@
2323
"@reduxjs/toolkit": ["packages/toolkit/src/index.ts"],
2424
"@reduxjs/toolkit/query": ["packages/toolkit/src/query/index.ts"],
2525
"@reduxjs/toolkit/query/react": [
26-
"packages/toolkit/src/query/react/index.ts",
26+
"packages/toolkit/src/query/react/index.ts"
2727
],
2828
"@reduxjs/toolkit/dist/query/*": ["packages/toolkit/src/query/*"],
2929
"@virtual/*": ["docs/virtual/*"],
3030
"your-cool-library": ["docs/virtual/your-cool-library/index.ts"],
3131
"redux-logger": ["docs/virtual/redux-logger/index.ts"],
32-
"petstore-api.generated": [
33-
"docs/virtual/petstore-api.generated/index.ts",
34-
],
35-
},
36-
},
32+
"petstore-api.generated": ["docs/virtual/petstore-api.generated/index.ts"]
33+
}
34+
}
3735
}

docs/usage/nextjs.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ It is worth noting that initializing the store with a `useEffect` would not work
380380

381381
### Caching
382382

383-
The App Router has four seperate caches including `fetch` request and route caches. The most likely cache to cause issues is the route cache. If you have an application that accepts login you may have routes (e.g. the home route, `/`) that render different data based on the user you will need to disable the route cache by using the [`dynamic` export from the route handler](https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic):
383+
The App Router has four separate caches including `fetch` request and route caches. The most likely cache to cause issues is the route cache. If you have an application that accepts login you may have routes (e.g. the home route, `/`) that render different data based on the user you will need to disable the route cache by using the [`dynamic` export from the route handler](https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic):
384384

385385
```ts
386386
export const dynamic = 'force-dynamic'
Binary file not shown.

examples/action-listener/counter/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"resolveJsonModule": true,
1515
"isolatedModules": true,
1616
"noEmit": true,
17-
"jsx": "react-jsx",
17+
"jsx": "react-jsx"
1818
},
19-
"include": ["src"],
19+
"include": ["src"]
2020
}

examples/publish-ci/cra4/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"@types/react": "^18.0.26",
5151
"@types/react-dom": "^18.0.10",
5252
"playwright": "^1.31.1",
53-
"prettier": "^3.2.4",
53+
"prettier": "^3.2.5",
5454
"serve": "^14.2.0",
5555
"typescript": "^4.9.4"
5656
}

examples/publish-ci/cra4/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"resolveJsonModule": true,
1515
"isolatedModules": true,
1616
"noEmit": true,
17-
"jsx": "react-jsx",
17+
"jsx": "react-jsx"
1818
},
19-
"include": ["src"],
19+
"include": ["src"]
2020
}

0 commit comments

Comments
 (0)