When you call createAsyncThunk
, it will return an Action Creator
which redux-thunk
can use.
You should use the function like this:
const actionCreator = createAsyncThunk('prefix', async function echo(text) {
return text;
});
// app.js
import { unwrapResult } from '@reduxjs/toolkit';
function App() {
const dispatch = useDispatch();
return (
<button
onClick={() => {
dispatch(actionCreator('233'))
.then(unwrapResult)
.then((result) => {
console.log(result); // => 233
})
.catch((error) => {
console.error(error); // if there is an error
});
}}
>
Echo
</button>
);
}
This is what unwrapResult
does:
// @reduxjs/toolkit
export function unwrapResult<R extends ActionTypesWithOptionalErrorAction>(
returned: R
): PayloadForActionTypesExcludingErrorActions<R> {
if ('error' in returned) {
throw returned.error
}
return (returned as any).payload
}
To handle errors, there are two ways. The first is to throw them directly, and the second is to return rejectWithValue
.
async function echo(text, { rejectWithValue }) {
if (Math.random() < 0.5) {
// throw new Error('error msg');
// return rejectWithValue('error value');
}
return text;
}
dispatch(actionCreator('233'))
.then(unwrapResult)
.then((result) => {
console.log(result); // => 233
})
.catch((error) => {
// by throw new Error('error msg');
console.log(error.message); // => error msg
// by return rejectWithValue('error value')
console.log(error); // => error value
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…