Skip to content

Commit a11aab2

Browse files
msutkowskiphryneas
authored andcommitted
Update usage-with-typescript to have consistent naming with createAsyncThunk examples
1 parent 67f7b96 commit a11aab2

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

docs/usage/usage-with-typescript.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -454,20 +454,26 @@ interface MyKnownError {
454454
errorMessage: string
455455
// ...
456456
}
457+
interface UserAttributes {
458+
id: string
459+
first_name: string
460+
last_name: string
461+
email: string
462+
}
457463

458-
const updateUserById = createAsyncThunk<
464+
const updateUser = createAsyncThunk<
459465
// Return type of the payload creator
460466
MyData,
461467
// First argument to the payload creator
462-
{ id: string; first_name: string; last_name: string; email: string },
468+
UserAttributes,
463469
// Types for ThunkAPI
464470
{
465471
extra: {
466472
jwt: string
467473
}
468474
rejectValue: MyKnownError
469475
}
470-
>('users/updateById', async (user, thunkApi) => {
476+
>('users/update', async (user, thunkApi) => {
471477
const { id, ...userData } = user
472478
const response = await fetch(`https://reqres.in/api/users/${id}`, {
473479
method: 'PUT',
@@ -499,12 +505,12 @@ const usersSlice = createSlice({
499505
},
500506
reducers: {},
501507
extraReducers: builder => {
502-
builder.addCase(updateUserById.fulfilled, (state, { payload }) => {
508+
builder.addCase(updateUser.fulfilled, (state, { payload }) => {
503509
state.entities[payload.id] = payload
504510
})
505-
builder.addCase(updateUserById.rejected, (state, action) => {
511+
builder.addCase(updateUser.rejected, (state, action) => {
506512
if (action.payload) {
507-
// Since we passed in `MyKnownError` to `rejectType` in `updateUserById`, the type information will be available here.
513+
// Since we passed in `MyKnownError` to `rejectType` in `updateUser`, the type information will be available here.
508514
state.error = action.payload.errorMessage
509515
} else {
510516
state.error = action.error
@@ -517,16 +523,16 @@ const usersSlice = createSlice({
517523
- In a component
518524

519525
```ts
520-
const updateUser = async userData => {
521-
const { id, ...userFields } = userData
522-
const resultAction = await dispatch(updateUserById(userFields))
526+
const handleUpdateUser = async userData => {
527+
const resultAction = await dispatch(updateUser(userData))
523528
if (updateUser.fulfilled.match(resultAction)) {
524529
const user = unwrapResult(resultAction)
525530
showToast('success', `Updated ${user.name}`)
526531
} else {
527532
if (resultAction.payload) {
528-
// Since we passed in `MyKnownError` to `rejectType` in `updateUserById`, the type information will be available here.
529-
showToast('error', `Update failed: ${rersultAction.payload.errorMessage}`)
533+
// Since we passed in `MyKnownError` to `rejectType` in `updateUser`, the type information will be available here.
534+
// Note: this would also be a good place to do any handling that relies on the `rejectedWithValue` payload, such as setting field errors
535+
showToast('error', `Update failed: ${resultAction.payload.errorMessage}`)
530536
} else {
531537
showToast('error', `Update failed: ${resultAction.error.message}`)
532538
}

0 commit comments

Comments
 (0)