You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api/createAsyncThunk.mdx
+3-3
Original file line number
Diff line number
Diff line change
@@ -96,9 +96,9 @@ The logic in the `payloadCreator` function may use any of these values as needed
96
96
97
97
An object with the following optional fields:
98
98
99
-
-`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.
99
+
-`condition(arg, { getState, extra } ): boolean | Promise<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.
100
100
-`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`.
101
-
-`idGenerator(): string`: a function to use when generating the `requestId` for the request sequence. Defaults to use [nanoid](./otherExports.mdx/#nanoid).
101
+
-`idGenerator(arg): string`: a function to use when generating the `requestId` for the request sequence. Defaults to use [nanoid](./otherExports.mdx/#nanoid), but you can implement your own ID generation logic.
102
102
-`serializeError(error: unknown) => any` to replace the internal `miniSerializeError` method with your own serialization logic.
103
103
-`getPendingMeta({ arg, requestId }, { getState, extra }): any`: a function to create an object that will be merged into the `pendingAction.meta` field.
If you need to cancel a thunk before the payload creator is called, you may provide a `condition` callback as an option after the payload creator. The callback will receive the thunk argument and an object with `{getState, extra}` as parameters, and use those to decide whether to continue or not. If the execution should be canceled, the `condition` callback should return a literal `false` value:
360
+
If you need to cancel a thunk before the payload creator is called, you may provide a `condition` callback as an option after the payload creator. The callback will receive the thunk argument and an object with `{getState, extra}` as parameters, and use those to decide whether to continue or not. If the execution should be canceled, the `condition` callback should return a literal `false` value or a promise that should resolve to `false`. If a promise is returned, the thunk waits for it to get fulfilled before dispatching the `pending` action, otherwise it proceeds with dispatching synchronously.
The reducer will have a `getInitialState` function attached that will return the initial state when called. This may be useful for tests or usage with React's `useReducer` hook:
129
+
130
+
```js
131
+
constcounterReducer=createReducer(0, {
132
+
increment: (state, action) => state +action.payload,
133
+
decrement: (state, action) => state -action.payload,
Copy file name to clipboardExpand all lines: docs/api/createSlice.mdx
+4-1
Original file line number
Diff line number
Diff line change
@@ -71,6 +71,8 @@ function createSlice({
71
71
72
72
Theinitialstatevalueforthissliceofstate.
73
73
74
+
Thismayalsobea"lazy initializer"function, which should return an initial state value when called. This will be used whenever the reducer is called with `undefined` as its state value, and is primarily useful for cases like reading initial state from `localStorage`.
75
+
74
76
### `name`
75
77
76
78
Astringnameforthissliceofstate. Generatedactiontypeconstants will use this as a prefix.
@@ -196,7 +198,8 @@ We recommend using the `builder callback` API as the default, especially if you
Copy file name to clipboardExpand all lines: docs/rtk-query/api/created-api/api-slice-utils.mdx
+60-5
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,19 @@
1
1
---
2
-
id: cache-management-utils
3
-
title: 'API Slices: Cache Management'
4
-
sidebar_label: Cache Management Utils
2
+
id: api-slice-utils
3
+
title: 'API Slices: Utilities'
4
+
sidebar_label: API Slice Utilities
5
5
hide_title: true
6
6
---
7
7
8
8
9
9
10
-
# API Slices: Cache Management Utilities
10
+
# API Slices: Utilities
11
11
12
-
The API slice object includes cache management utilities that are used for implementing [optimistic updates](../../usage/manual-cache-updates.mdx#optimistic-updates). These are included in a `util` field inside the slice object.
12
+
The API slice object includes various utilities that can be used for cache management,
13
+
such as implementing [optimistic updates](../../usage/manual-cache-updates.mdx#optimistic-updates),
14
+
as well implementing [server side rendering](../../usage/server-side-rendering.mdx).
15
+
16
+
These are included in a `util` field inside the slice object.
13
17
14
18
### `updateQueryData`
15
19
@@ -244,3 +248,54 @@ Note that [hooks](./hooks.mdx) also track state in local component state and mig
- `options`: A set of options that control the subscription behavior of the hook
364
+
- `options`: A set of options that control the subscription behavior of the hook:
365
+
- `selectFromResult`: A callback that can be used to customize the mutation result returned as the second item in the tuple
366
+
- `fixedCacheKey`: An optional string used to enable shared results across hook instances
360
367
361
368
- **Returns**: A tuple containing:
362
369
- `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
363
-
- `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
370
+
- `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.
371
+
Additionally, this object will contain
372
+
- a `reset` method to reset the hook back to it's original state and remove the current result from the cache
373
+
- an `originalArgs` property that contains the argument passed to the last call of the `trigger` function.
364
374
365
375
#### Description
366
376
@@ -388,7 +398,8 @@ type UseQueryStateOptions = {
388
398
typeUseQueryStateResult<T> = {
389
399
// Base query state
390
400
originalArgs?:unknown// Arguments passed to the query
391
-
data?:T// Returned result if present
401
+
data?:T// The latest returned result regardless of hook arg, if present
402
+
currentData?:T// The latest returned result for the current hook arg, if present
392
403
error?:unknown// Error result if present
393
404
requestId?:string// A string generated by RTK Query
394
405
endpointName?:string// The name of the given endpoint for the query
@@ -459,9 +470,8 @@ type UseQuerySubscriptionResult = {
459
470
## `useLazyQuery`
460
471
461
472
```tstitle="Accessing a useLazyQuery hook"no-transpile
arg:unknown// Whatever argument was provided to the query
497
+
requestId:string// A string generated by RTK Query
498
+
subscriptionOptions:SubscriptionOptions// The values used for the query subscription
499
+
abort: () =>void// A method to cancel the query promise
500
+
unwrap: () =>Promise<T> // A method to unwrap the query call and provide the raw response/error
501
+
unsubscribe: () =>void// A method used to manually unsubscribe from the query results
502
+
refetch: () =>void// A method used to re-run the query. In most cases when using a lazy query, you will never use this and should prefer to call the trigger again.
503
+
updateSubscriptionOptions: (options:SubscriptionOptions) () =>void// A method used to update the subscription options (eg. pollingInterval)
504
+
}
484
505
485
506
typeUseQueryStateResult<T> = {
486
507
// Base query state
487
508
originalArgs?:unknown// Arguments passed to the query
488
-
data?:T// Returned result if present
509
+
data?:T// The latest returned result regardless of trigger arg, if present
510
+
currentData?:T// The latest returned result for the trigger arg, if present
489
511
error?:unknown// Error result if present
490
512
requestId?:string// A string generated by RTK Query
491
513
endpointName?:string// The name of the given endpoint for the query
@@ -520,9 +542,8 @@ type UseLazyQueryLastPromiseInfo = {
520
542
## `useLazyQuerySubscription`
521
543
522
544
```tstitle="Accessing a useLazyQuerySubscription hook"no-transpile
0 commit comments