Skip to content

Commit 129bf5a

Browse files
committed
Update createAsyncThunk.mdx
1 parent 5c76630 commit 129bf5a

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

docs/api/createAsyncThunk.mdx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ Redux Toolkit's [**RTK Query data fetching API**](../rtk-query/overview.md) is a
2626
Sample usage:
2727

2828
```ts {5-11,22-25,30}
29+
interface userAPI {
30+
fetchById: (userId: number) => Promise<any>
31+
}
32+
2933
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'
3034
import { userAPI } from './userAPI'
3135

@@ -212,7 +216,7 @@ type RejectedWithValue = <ThunkArg, RejectedValue>(
212216
213217
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)):
214218
215-
```js {2,6,14,23}
219+
```ts {2,6,14,23}
216220
const reducer1 = createReducer(initialState, {
217221
[fetchUserById.fulfilled]: (state, action) => {},
218222
})
@@ -246,7 +250,7 @@ const reducer4 = createSlice({
246250

247251
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:
248252

249-
```js
253+
```ts
250254
const onClick = () => {
251255
dispatch(fetchUserById(userId)).then(() => {
252256
// do additional work
@@ -346,7 +350,7 @@ If you need to customize the contents of the `rejected` action, you should catch
346350

347351
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.
348352

349-
```js
353+
```ts
350354
const updateUser = createAsyncThunk(
351355
'users/update',
352356
async (userData, { rejectWithValue }) => {
@@ -369,7 +373,7 @@ const updateUser = createAsyncThunk(
369373

370374
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.
371375

372-
```js
376+
```ts
373377
const fetchUserById = createAsyncThunk(
374378
'users/fetchByIdStatus',
375379
async (userId: number, thunkAPI) => {
@@ -511,7 +515,7 @@ const fetchUserById = createAsyncThunk(
511515

512516
- Requesting a user by ID, with loading state, and only one request at a time:
513517

514-
```js
518+
```ts
515519
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'
516520
import { userAPI } from './userAPI'
517521

0 commit comments

Comments
 (0)