我有一个基于 redux-toolkit 构建的简单应用程序。我正在调度 createProduct 操作,该操作运行良好。我想在 createProduct 操作后导航到 /products/ 页面表单 /products/new 页面。我如何使用导航(react-router-dom)来做到这一点。
我尝试了这个内部动作但失败了
[createProduct.fulfilled]: (state, { payload }) => {
toast.success('Product Created Successfully!');
const navigate = useNavigate()
navigate('/products')
return {
...state,
loading: false,
products: state.products ? [...state.products, payload.product] : [payload.product]
};
},
Run Code Online (Sandbox Code Playgroud)
我也尝试将导航传递给有效负载,但遇到了此错误:
You may not call store.getState() while the reducer is executing. The reducer has already received the state as an argument. Pass it down from the top reducer instead of reading it from the store.
Run Code Online (Sandbox Code Playgroud)
我正在像这样调度 createProduct
const handleSubmit …Run Code Online (Sandbox Code Playgroud)