[英]react redux dispatch to reducer in createslice not work
我的朋友們,我開發了 react redux 工具包,一切正常,但是在 function 中,調度不起作用發送到 dispatchUserBlockingUpdate function。我的問題在哪里?
我看 changeProfitSortType function 獲取數據時需要發送調度(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
不是異步的 function,所以await dispatch(changeProfitSort(status));
沒有意義。
您根本不需要changeProfitSortType
function。 您可以在組件中調用dispatch(changeProfitSort(data.status))
而不是dispatch(changeProfitSortType(data))
。
如果data
來自異步 function 那么您可能需要使用createAsyncThunk
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.