Skip to content

Commit e9ad9ce

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

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

docs/api/createAsyncThunk.mdx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ Redux Toolkit's [**RTK Query data fetching API**](../rtk-query/overview.md) is a
2525

2626
Sample usage:
2727

28-
```ts {5-11,22-25,30}
29-
interface userAPI {
30-
fetchById: (userId: number) => Promise<any>
31-
}
32-
28+
```ts no-transpile {5-11,22-25,30}
3329
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'
3430
import { userAPI } from './userAPI'
3531

@@ -373,7 +369,7 @@ const updateUser = createAsyncThunk(
373369

374370
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.
375371

376-
```ts
372+
```ts no-transpile
377373
const fetchUserById = createAsyncThunk(
378374
'users/fetchByIdStatus',
379375
async (userId: number, thunkAPI) => {
@@ -401,7 +397,7 @@ If you want to cancel your running thunk before it has finished, you can use the
401397

402398
A real-life example of that would look like this:
403399

404-
```ts
400+
```ts no-transpile
405401
// file: store.ts noEmit
406402
import { configureStore, Reducer } from '@reduxjs/toolkit'
407403
import { useDispatch } from 'react-redux'
@@ -443,7 +439,7 @@ Additionally, your `payloadCreator` can use the `AbortSignal` it is passed via `
443439

444440
The `fetch` api of modern browsers already comes with support for an `AbortSignal`:
445441

446-
```ts
442+
```ts no-transpile
447443
import { createAsyncThunk } from '@reduxjs/toolkit'
448444

449445
const fetchUserById = createAsyncThunk(
@@ -463,7 +459,7 @@ const fetchUserById = createAsyncThunk(
463459

464460
You can use the `signal.aborted` property to regularly check if the thunk has been aborted and in that case stop costly long-running work:
465461

466-
```ts
462+
```ts no-transpile
467463
import { createAsyncThunk } from '@reduxjs/toolkit'
468464

469465
const readStream = createAsyncThunk(
@@ -492,7 +488,7 @@ const readStream = createAsyncThunk(
492488
You can also call `signal.addEventListener('abort', callback)` to have logic inside the thunk be notified when `promise.abort()` was called.
493489
This can for example be used in conjunction with an axios `CancelToken`:
494490

495-
```ts
491+
```ts no-transpile
496492
import { createAsyncThunk } from '@reduxjs/toolkit'
497493
import axios from 'axios'
498494

@@ -515,7 +511,7 @@ const fetchUserById = createAsyncThunk(
515511

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

518-
```ts
514+
```ts no-transpile
519515
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'
520516
import { userAPI } from './userAPI'
521517

0 commit comments

Comments
 (0)