如何解决将 redux 调度反应到创建切片中的 reducer 不起作用
我的朋友们, 我开发 react redux 工具包切片一切正常,但在函数调度中不起作用发送到 react-dom.development.js 中的 dispatchUserBlockingUpdate 函数我的问题在哪里?
我看 changeProfitSortType 函数在需要发送 dispatch(changeProfitSort) 时获取数据不起作用!
这是:sortingSlice.js
import {createSlice} from "@reduxjs/toolkit";
export const sortingSlice = createSlice({
name: 'sorting',initialState: {
profitSortType: false,},reducers: {
changeProfitSort: (state,action) => {
state.profitSortType = action.payload;
//return { ...state,profitSortType: action.payload }
},});
export const { changeProfitSort } = sortingSlice.actions;
export const changeProfitSortType = (data) => async dispatch => {
console.log(data);
let status = data.status;
console.log(status);
await dispatch(changeProfitSort(status));
}
export default sortingSlice.reducer;
解决方法
你的sortingSlice
很好。
changeProfitSort
不是异步函数,所以 await dispatch(changeProfitSort(status));
没有意义。
您根本不需要 changeProfitSortType
函数。您可以在组件中调用 dispatch(changeProfitSort(data.status))
而不是 dispatch(changeProfitSortType(data))
。
如果 data
来自异步函数,那么您可能需要使用 createAsyncThunk
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。