-1

我的朋友们,我开发了 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;
4

1 回答 1

0

sortingSlice的很好。

changeProfitSort不是异步函数,所以await dispatch(changeProfitSort(status));没有意义。

你根本不需要这个changeProfitSortType功能。您可以只调用dispatch(changeProfitSort(data.status))您的组件而不是dispatch(changeProfitSortType(data)).

如果data来自异步函数,那么您可能想要使用createAsyncThunk.

于 2021-05-24T21:53:16.617 回答