Skip to content

Commit 639ec4d

Browse files
committed
Merge branch 'v2.0-integration' into v2.0-docs
2 parents cb31d8c + 430a8e4 commit 639ec4d

File tree

73 files changed

+610
-430
lines changed

Some content is hidden

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

73 files changed

+610
-430
lines changed

docs/api/configureStore.mdx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ to the store setup for a better development experience.
2020

2121
interface ConfigureStoreOptions<
2222
S = any,
23-
A extends Action = AnyAction,
24-
M extends Middlewares<S> = Middlewares<S>,
25-
E extends Enhancers = Enhancers,
23+
A extends Action = UnknownAction,
24+
M extends Tuple<Middlewares<S>> = Tuple<Middlewares<S>>
25+
E extends Tuple<Enhancers> = Tuple<Enhancers>,
2626
P = S
2727
> {
2828
/**
@@ -63,8 +63,11 @@ interface ConfigureStoreOptions<
6363

6464
function configureStore<
6565
S = any,
66-
A extends Action = AnyAction,
67-
M extends Middlewares<S> = Middlewares<S>,
66+
A extends Action = UnknownAction,
67+
M extends Tuple<Middlewares<S>> = Tuple<Middlewares<S>>
68+
E extends Tuple<Enhancers> = Tuple<Enhancers>,
69+
P = S
70+
>(options: ConfigureStoreOptions<S, A, M, E, P>): EnhancedStore<S, A, M, E>
6871
```
6972

7073
### `reducer`

docs/api/createDynamicMiddleware.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ The "dynamic middleware instance" returned from `createDynamicMiddleware` is an
8080
```ts no-transpile
8181
export type DynamicMiddlewareInstance<
8282
State = unknown,
83-
Dispatch extends ReduxDispatch<AnyAction> = ReduxDispatch<AnyAction>
83+
Dispatch extends ReduxDispatch<UnknownAction> = ReduxDispatch<UnknownAction>
8484
> = {
8585
middleware: DynamicMiddleware<State, Dispatch>
8686
addMiddleware: AddMiddleware<State, Dispatch>
@@ -131,7 +131,7 @@ _These depend on having `react-redux` installed._
131131
```ts no-transpile
132132
interface ReactDynamicMiddlewareInstance<
133133
State = any,
134-
Dispatch extends ReduxDispatch<AnyAction> = ReduxDispatch<AnyAction>
134+
Dispatch extends ReduxDispatch<UnknownAction> = ReduxDispatch<UnknownAction>
135135
> extends DynamicMiddlewareInstance<State, Dispatch> {
136136
createDispatchWithMiddlewareHook: CreateDispatchWithMiddlewareHook<
137137
State,

docs/api/createEntityAdapter.mdx

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,11 @@ export type Comparer<T> = (a: T, b: T) => number
120120

121121
export type IdSelector<T> = (model: T) => EntityId
122122

123-
export interface DictionaryNum<T> {
124-
[id: number]: T | undefined
125-
}
126-
127-
export interface Dictionary<T> extends DictionaryNum<T> {
128-
[id: string]: T | undefined
129-
}
130-
131123
export type Update<T> = { id: EntityId; changes: Partial<T> }
132124

133125
export interface EntityState<T> {
134126
ids: EntityId[]
135-
entities: Dictionary<T>
127+
entities: Record<EntityId, T>
136128
}
137129

138130
export interface EntityDefinition<T> {
@@ -185,7 +177,7 @@ export interface EntityStateAdapter<T> {
185177

186178
export interface EntitySelectors<T, V> {
187179
selectIds: (state: V) => EntityId[]
188-
selectEntities: (state: V) => Dictionary<T>
180+
selectEntities: (state: V) => Record<EntityId, T>
189181
selectAll: (state: V) => T[]
190182
selectTotal: (state: V) => number
191183
selectById: (state: V, id: EntityId) => T | undefined
@@ -287,9 +279,35 @@ The entity adapter will contain a `getSelectors()` function that returns a set o
287279

288280
Each selector function will be created using the `createSelector` function from Reselect, to enable memoizing calculation of the results.
289281

282+
:::tip
283+
284+
The `createSelector` instance used can be replaced, by passing it as part of the options object (second parameter):
285+
286+
```js
287+
import {
288+
createDraftSafeSelectorCreator,
289+
weakMapMemoize,
290+
} from '@reduxjs/toolkit'
291+
292+
const createWeakMapDraftSafeSelector =
293+
createDraftSafeSelectorCreator(weakMapMemoize)
294+
295+
const simpleSelectors = booksAdapter.getSelectors(undefined, {
296+
createSelector: createWeakMapDraftSafeSelector,
297+
})
298+
299+
const globalizedSelectors = booksAdapter.getSelectors((state) => state.books, {
300+
createSelector: createWeakMapDraftSafeSelector,
301+
})
302+
```
303+
304+
If no instance is passed, it will default to [`createDraftSafeSelector`](./createSelector#createDraftSafeSelector).
305+
306+
:::
307+
290308
Because selector functions are dependent on knowing where in the state tree this specific entity state object is kept, `getSelectors()` can be called in two ways:
291309

292-
- If called without any arguments, it returns an "unglobalized" set of selector functions that assume their `state` argument is the actual entity state object to read from.
310+
- If called without any arguments (or with undefined as the first parameter), it returns an "unglobalized" set of selector functions that assume their `state` argument is the actual entity state object to read from.
293311
- It may also be called with a selector function that accepts the entire Redux state tree and returns the correct entity state object.
294312

295313
For example, the entity state for a `Book` type might be kept in the Redux state tree as `state.books`. You can use `getSelectors()` to read from that state in two ways:

docs/api/createListenerMiddleware.mdx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ The "listener middleware instance" returned from `createListenerMiddleware` is a
120120
```ts no-transpile
121121
interface ListenerMiddlewareInstance<
122122
State = unknown,
123-
Dispatch extends ThunkDispatch<State, unknown, AnyAction> = ThunkDispatch<
123+
Dispatch extends ThunkDispatch<State, unknown, UnknownAction> = ThunkDispatch<
124124
State,
125125
unknown,
126-
AnyAction
126+
UnknownAction
127127
>,
128128
ExtraArgument = unknown
129129
> {
@@ -181,7 +181,7 @@ interface AddListenerOptions {
181181
effect: (action: Action, listenerApi: ListenerApi) => void | Promise<void>
182182
}
183183

184-
type ListenerPredicate<Action extends AnyAction, State> = (
184+
type ListenerPredicate<Action extends ReduxAction, State> = (
185185
action: Action,
186186
currentState?: State,
187187
originalState?: State
@@ -321,7 +321,7 @@ The `listenerApi` object is the second argument to each listener callback. It co
321321
```ts no-transpile
322322
export interface ListenerEffectAPI<
323323
State,
324-
Dispatch extends ReduxDispatch<AnyAction>,
324+
Dispatch extends ReduxDispatch<UnknownAction>,
325325
ExtraArgument = unknown
326326
> extends MiddlewareAPI<Dispatch, State> {
327327
// NOTE: MiddlewareAPI contains `dispatch` and `getState` already
@@ -572,12 +572,12 @@ Listeners can use the `condition` and `take` methods in `listenerApi` to wait un
572572
The signatures are:
573573

574574
```ts no-transpile
575-
type ConditionFunction<Action extends AnyAction, State> = (
575+
type ConditionFunction<Action extends ReduxAction, State> = (
576576
predicate: ListenerPredicate<Action, State> | (() => boolean),
577577
timeout?: number
578578
) => Promise<boolean>
579579

580-
type TakeFunction<Action extends AnyAction, State> = (
580+
type TakeFunction<Action extends ReduxAction, State> = (
581581
predicate: ListenerPredicate<Action, State> | (() => boolean),
582582
timeout?: number
583583
) => Promise<[Action, State, State] | null>

docs/api/createSelector.mdx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ All selectors created by `entityAdapter.getSelectors` are "draft safe" selectors
3939

4040
Example:
4141

42-
```js
42+
```ts no-transpile
4343
const selectSelf = (state: State) => state
4444
const unsafeSelector = createSelector(selectSelf, (state) => state.value)
4545
const draftSafeSelector = createDraftSafeSelector(
@@ -64,3 +64,25 @@ After executing that, `unsafe1` and `unsafe2` will be of the same value, because
6464
executed on the same object - but `safe2` will actually be different from `safe1` (with the updated value of `2`),
6565
because the safe selector detected that it was executed on a Immer draft object and recalculated using the current
6666
value instead of returning a cached value.
67+
68+
:::tip `createDraftSafeSelectorCreator`
69+
70+
RTK also exports a `createDraftSafeSelectorCreator` function, the "draft safe" equivalent of [`createSelectorCreator`](https://github.com/reduxjs/reselect#createselectorcreatormemoize-memoizeoptions).
71+
72+
```ts no-transpile
73+
import {
74+
createDraftSafeSelectorCreator,
75+
weakMapMemoize,
76+
} from '@reduxjs/toolkit'
77+
78+
const createWeakMapDraftSafeSelector =
79+
createDraftSafeSelectorCreator(weakMapMemoize)
80+
81+
const selectSelf = (state: State) => state
82+
const draftSafeSelector = createWeakMapDraftSafeSelector(
83+
selectSelf,
84+
(state) => state.value
85+
)
86+
```
87+
88+
:::

docs/api/matching-utilities.mdx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ A higher-order function that returns a type guard function that may be used to c
6060

6161
```ts title="isAsyncThunkAction usage"
6262
import { isAsyncThunkAction } from '@reduxjs/toolkit'
63-
import type { AnyAction } from '@reduxjs/toolkit'
63+
import type { UnknownAction } from '@reduxjs/toolkit'
6464
import { requestThunk1, requestThunk2 } from '@virtual/matchers'
6565

6666
const isARequestAction = isAsyncThunkAction(requestThunk1, requestThunk2)
6767

68-
function handleRequestAction(action: AnyAction) {
68+
function handleRequestAction(action: UnknownAction) {
6969
if (isARequestAction(action)) {
7070
// action is an action dispatched by either `requestThunk1` or `requestThunk2`
7171
}
@@ -78,12 +78,12 @@ A higher-order function that returns a type guard function that may be used to c
7878

7979
```ts title="isPending usage"
8080
import { isPending } from '@reduxjs/toolkit'
81-
import type { AnyAction } from '@reduxjs/toolkit'
81+
import type { UnknownAction } from '@reduxjs/toolkit'
8282
import { requestThunk1, requestThunk2 } from '@virtual/matchers'
8383

8484
const isAPendingAction = isPending(requestThunk1, requestThunk2)
8585

86-
function handlePendingAction(action: AnyAction) {
86+
function handlePendingAction(action: UnknownAction) {
8787
if (isAPendingAction(action)) {
8888
// action is a pending action dispatched by either `requestThunk1` or `requestThunk2`
8989
}
@@ -96,12 +96,12 @@ A higher-order function that returns a type guard function that may be used to c
9696

9797
```ts title="isFulfilled usage"
9898
import { isFulfilled } from '@reduxjs/toolkit'
99-
import type { AnyAction } from '@reduxjs/toolkit'
99+
import type { UnknownAction } from '@reduxjs/toolkit'
100100
import { requestThunk1, requestThunk2 } from '@virtual/matchers'
101101

102102
const isAFulfilledAction = isFulfilled(requestThunk1, requestThunk2)
103103

104-
function handleFulfilledAction(action: AnyAction) {
104+
function handleFulfilledAction(action: UnknownAction) {
105105
if (isAFulfilledAction(action)) {
106106
// action is a fulfilled action dispatched by either `requestThunk1` or `requestThunk2`
107107
}
@@ -114,12 +114,12 @@ A higher-order function that returns a type guard function that may be used to c
114114

115115
```ts title="isRejected usage"
116116
import { isRejected } from '@reduxjs/toolkit'
117-
import type { AnyAction } from '@reduxjs/toolkit'
117+
import type { UnknownAction } from '@reduxjs/toolkit'
118118
import { requestThunk1, requestThunk2 } from '@virtual/matchers'
119119

120120
const isARejectedAction = isRejected(requestThunk1, requestThunk2)
121121

122-
function handleRejectedAction(action: AnyAction) {
122+
function handleRejectedAction(action: UnknownAction) {
123123
if (isARejectedAction(action)) {
124124
// action is a rejected action dispatched by either `requestThunk1` or `requestThunk2`
125125
}
@@ -132,15 +132,15 @@ A higher-order function that returns a type guard function that may be used to c
132132

133133
```ts title="isRejectedWithValue usage"
134134
import { isRejectedWithValue } from '@reduxjs/toolkit'
135-
import type { AnyAction } from '@reduxjs/toolkit'
135+
import type { UnknownAction } from '@reduxjs/toolkit'
136136
import { requestThunk1, requestThunk2 } from '@virtual/matchers'
137137

138138
const isARejectedWithValueAction = isRejectedWithValue(
139139
requestThunk1,
140140
requestThunk2
141141
)
142142

143-
function handleRejectedWithValueAction(action: AnyAction) {
143+
function handleRejectedWithValueAction(action: UnknownAction) {
144144
if (isARejectedWithValueAction(action)) {
145145
// action is a rejected action dispatched by either `requestThunk1` or `requestThunk2`
146146
// where rejectWithValue was used

docs/rtk-query/api/createApi.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const { useGetPokemonByNameQuery } = pokemonApi
5757
baseQuery(args: InternalQueryArgs, api: BaseQueryApi, extraOptions?: DefinitionExtraOptions): any;
5858
endpoints(build: EndpointBuilder<InternalQueryArgs, TagTypes>): Definitions;
5959
extractRehydrationInfo?: (
60-
action: AnyAction,
60+
action: UnknownAction,
6161
{
6262
reducerPath,
6363
}: {
@@ -88,7 +88,7 @@ export const { useGetPokemonByNameQuery } = pokemonApi
8888
- `dispatch` - The `store.dispatch` method for the corresponding Redux store
8989
- `getState` - A function that may be called to access the current store state
9090
- `extra` - Provided as thunk.extraArgument to the configureStore getDefaultMiddleware option.
91-
- `endpoint` - The name of the endpoint.
91+
- `endpoint` - The name of the endpoint.
9292
- `type` - Type of request (`query` or `mutation`).
9393
- `forced` - Indicates if a query has been forced.
9494
- `extraOptions` - The value of the optional `extraOptions` property provided for a given endpoint

docs/rtk-query/api/created-api/api-slice-utils.mdx

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const updateQueryData = (
3030
endpointName: string,
3131
args: any,
3232
updateRecipe: (draft: Draft<CachedState>) => void
33-
) => ThunkAction<PatchCollection, PartialState, any, AnyAction>;
33+
) => ThunkAction<PatchCollection, PartialState, any, UnknownAction>;
3434

3535
interface PatchCollection {
3636
patches: Patch[];
@@ -119,7 +119,7 @@ const upsertQueryData = <T>(
119119
endpointName: string,
120120
args: any,
121121
newEntryData: T
122-
) => ThunkAction<Promise<CacheEntry<T>>, PartialState, any, AnyAction>;
122+
) => ThunkAction<Promise<CacheEntry<T>>, PartialState, any, UnknownAction>;
123123
```
124124

125125
- **Parameters**
@@ -156,7 +156,7 @@ const patchQueryData = (
156156
endpointName: string,
157157
args: any
158158
patches: Patch[]
159-
) => ThunkAction<void, PartialState, any, AnyAction>;
159+
) => ThunkAction<void, PartialState, any, UnknownAction>;
160160
```
161161

162162
- **Parameters**
@@ -203,7 +203,7 @@ const prefetch = (
203203
endpointName: string,
204204
arg: any,
205205
options: PrefetchOptions
206-
) => ThunkAction<void, any, any, AnyAction>;
206+
) => ThunkAction<void, any, any, UnknownAction>;
207207
```
208208

209209
- **Parameters**
@@ -229,42 +229,44 @@ dispatch(api.util.prefetch('getPosts', undefined, { force: true }))
229229
```
230230

231231
### `selectInvalidatedBy`
232-
232+
233233
#### Signature
234-
234+
235235
```ts no-transpile
236-
function selectInvalidatedBy(
237-
state: RootState,
238-
tags: ReadonlyArray<TagDescription<string>>
239-
): Array<{
240-
endpointName: string
241-
originalArgs: any
242-
queryCacheKey: QueryCacheKey
243-
}>
236+
function selectInvalidatedBy(
237+
state: RootState,
238+
tags: ReadonlyArray<TagDescription<string>>
239+
): Array<{
240+
endpointName: string
241+
originalArgs: any
242+
queryCacheKey: QueryCacheKey
243+
}>
244244
```
245-
245+
246246
- **Parameters**
247247
- `state`: the root state
248248
- `tags`: a readonly array of invalidated tags, where the provided `TagDescription` is one of the strings provided to the [`tagTypes`](../createApi.mdx#tagtypes) property of the api. e.g.
249249
- `[TagType]`
250250
- `[{ type: TagType }]`
251251
- `[{ type: TagType, id: number | string }]`
252-
252+
253253
#### Description
254-
254+
255255
A function that can select query parameters to be invalidated.
256-
256+
257257
The function accepts two arguments
258-
- the root state and
259-
- the cache tags to be invalidated.
260-
258+
259+
- the root state and
260+
- the cache tags to be invalidated.
261+
261262
It returns an array that contains
262-
- the endpoint name,
263-
- the original args and
264-
- the queryCacheKey.
265-
263+
264+
- the endpoint name,
265+
- the original args and
266+
- the queryCacheKey.
267+
266268
#### Example
267-
269+
268270
```ts no-transpile
269271
dispatch(api.util.selectInvalidatedBy(state, ['Post']))
270272
dispatch(api.util.selectInvalidatedBy(state, [{ type: 'Post', id: 1 }]))

0 commit comments

Comments
 (0)