简体   繁体   中英

react redux dispatch to reducer in createslice not work

I friends, I develop react redux toolkit slice everything ok but in function dispatch not working send to dispatchUserBlockingUpdate function in react-dom.development.js where is the my problem?

I look changeProfitSortType function get data when need to send dispatch(changeProfitSort) not work !

This is: 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;

Your sortingSlice is fine.

changeProfitSort is not an asynchronous function, so await dispatch(changeProfitSort(status)); doesn't make sense.

You don't need the changeProfitSortType function at all. You can just call dispatch(changeProfitSort(data.status)) in your component instead of dispatch(changeProfitSortType(data)) .

If the data is coming from an asynchronous function then you might want to use createAsyncThunk .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM