我如何在 redux 中导出 reducer 对象

how can i export a reducer object in redux

我是 redux 的新手,我在我的 reducers 变量中创建了一个 addToBasket 和 removeBasket 对象,但是当我试图导出它以便我可以在另一个组件中使用它时,我收到一个错误 类型错误:无法解构 'basketSlice.action' 的 属性 'addToBasket',因为它未定义。我不知道我是否没有以正确的方式解构它,请有人帮忙,我不知道哪里做错了

这是我的代码

import { createSlice } from '@reduxjs/toolkit';

const initialState = {
    items: [],
}

export const basketSlice = createSlice({
    name: "basket",
    initialState,
    reducers: {
        addToBasket: (state, action) => {
            state.items = [...state.items, action.payload]
        },
        removeFromBasket: (state, action) => {},
    }
});

export const { addToBasket, removeFromBasket} = basketSlice.action;

export default basketSlice.reducer;

你打错了 - 是 actions,而不是 action

export const { addToBasket, removeFromBasket} = basketSlice.actions;