使用 createSlice 从 redux 中删除状态
Delete state from redux using createSlice
我有一个设置 'order' 初始状态的缩减程序 'setOrder',但是,我现在想创建一个从初始状态删除 'order' 的缩减程序。我该怎么做?
const checkoutSlice = createSlice({
name: 'checkout',
initialState: {
customer: {
firstName: '',
lastName: ''
},
order: {
wardrobeSize: ''
}
},
reducers: {
setCustomer(state, { payload }) {
state.customer = payload;
},
setOrder(state, { payload }) {
state.order = payload;
}
},
extraReducers: hydrateServerToClient('checkout')
});
export const { setCustomer, setOrder } = checkoutSlice.actions;
export default checkoutSlice.reducer;
我建议只为它分配一个空对象:
emptyOrder(state) {
state.order = {};
}
我有一个设置 'order' 初始状态的缩减程序 'setOrder',但是,我现在想创建一个从初始状态删除 'order' 的缩减程序。我该怎么做?
const checkoutSlice = createSlice({
name: 'checkout',
initialState: {
customer: {
firstName: '',
lastName: ''
},
order: {
wardrobeSize: ''
}
},
reducers: {
setCustomer(state, { payload }) {
state.customer = payload;
},
setOrder(state, { payload }) {
state.order = payload;
}
},
extraReducers: hydrateServerToClient('checkout')
});
export const { setCustomer, setOrder } = checkoutSlice.actions;
export default checkoutSlice.reducer;
我建议只为它分配一个空对象:
emptyOrder(state) {
state.order = {};
}