这里是一个例子,你可以如何做到这一点,正如评论 payload creator 的 rejectWithValue 是 第二个参数 的属性,并且有效负载创建者需要 返回 rejectWithValue 调用的结果,这里是一个例子:
// toggle reject
const reject = ((shouldReject) => () =>
(shouldReject = !shouldReject))(true);
// test thunk action creator
const testAsyncThunk = createAsyncThunk(
'some/test',
// arg is the argument passed to the action creator, can be ignored if not used
async (arg, { rejectWithValue }) => {
console.log('argument passed to action creator:', arg);
if (reject()) {
//return result of rejectWithValue call
return rejectWithValue('rejected');
}
return Promise.resolve('resolved');
}
);
当您调度使用 createAsyncThunk 创建的 thunk 时,除非您使用 unwrapResult,否则生成的 promise will not reject。
这是一个演示该行为的最小应用程序:
import React from 'react';
import ReactDOM from 'react-dom';
import {
createAsyncThunk,
unwrapResult,
} from '@reduxjs/toolkit';
import { Provider, useDispatch } from 'react-redux';
import {
createStore,
applyMiddleware,
compose,
} from 'redux';
// toggle reject
const reject = ((shouldReject) => () =>
(shouldReject = !shouldReject))(true);
// test thunk action creator
const testAsyncThunk = createAsyncThunk(
'some/test',
// arg is the argument passed to the action creator, can be ignored if not used
async (arg, { rejectWithValue }) => {
console.log('argument passed to action creator:', arg);
if (reject()) {
//return result of rejectWithValue call
return rejectWithValue('rejected value');
}
return Promise.resolve('resolved value');
}
);
const reducer = (state, { type, payload }) => {
return state;
};
//creating store with redux devtools and thunk middleware
const composeEnhancers =
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(
reducer,
{},
composeEnhancers(
applyMiddleware(
({ dispatch, getState }) => (next) => (action) =>
//minimal implementation of thunk middleware
typeof action === 'function'
? action(dispatch, getState)
: next(action)
)
)
);
const App = () => {
const dispatch = useDispatch();
return (
<button
onClick={() =>
dispatch(testAsyncThunk('argument passed'))
.then(
(resolved) => {
console.log('action resolved with', resolved);
return resolved;
},
(rejected) =>
// this never executes because promise returned
// by dispatch(tunkaction) will not reject
console.log('action rejected with:', rejected)
)
.then(
//after unwrap result you have a promise that will
// reject
unwrapResult
)
.catch((err) =>
console.log('rejected with...', err)
)
}
>
dispatch action
</button>
);
};
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);