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
// standard reducer logic, with auto-generated action types per reducer
47
57
},
@@ -202,7 +212,7 @@ type RejectedWithValue = <ThunkArg, RejectedValue>(
202
212
203
213
To handle these actions in your reducers, reference the action creators in `createReducer` or `createSlice` using either the object key notation or the "builder callback" notation. (Note that if you use TypeScript, you [should use the "builder callback" notation to ensure the types are inferred correctly](../usage/usage-with-typescript.md#type-safety-with-extrareducers)):
Thunks may return a value when dispatched. A common use case is to return a promise from the thunk, dispatch the thunk from a component, and then wait for the promise to resolve before doing additional work:
238
248
239
-
```js
249
+
```ts no-transpile
240
250
const onClick = () => {
241
251
dispatch(fetchUserById(userId)).then(() => {
242
252
// do additional work
@@ -336,7 +346,7 @@ If you need to customize the contents of the `rejected` action, you should catch
336
346
337
347
The `rejectWithValue` approach should also be used if your API response "succeeds", but contains some kind of additional error details that the reducer should know about. This is particularly common when expecting field-level validation errors from an API.
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.
361
371
362
-
```js
372
+
```ts no-transpile
363
373
const fetchUserById =createAsyncThunk(
364
374
'users/fetchByIdStatus',
365
-
async (userId, thunkAPI) => {
375
+
async (userId:number, thunkAPI) => {
366
376
const response =awaituserAPI.fetchById(userId)
367
377
returnresponse.data
368
378
},
@@ -387,7 +397,7 @@ If you want to cancel your running thunk before it has finished, you can use the
0 commit comments