我做了增量&递减计数器按钮,我使用redux来触发递增和递减的动作,但我得到了">Counter.js:11未捕获类型错误:无法读取未定义的属性(读取"addition'"("单击按钮时出错。
这是代码:-
store.js
const counterSlice = createSlice({
name: "counter",
initialState: {
countNumber: 0,
countToggle: true,
},
reducers: {
addition(state) {
state.countNumber++;
},
subtraction(state) {
state.countReducer--;
},
},
});
const store = configureStore({
reducer: counterSlice.reducer,
})
export const storeAction = store.actions;
export default store;
Counter.js
import { useDispatch, useSelector } from "react-redux";
import { storeAction } from "../redux-store/store";
const counter = useSelector((state) => state.countNumber);
const dispatch = useDispatch();
const addHandler = () => {
dispatch(storeAction.addition());
};
//jsx
<div className={classes.value}>{counter}</div>
<button onClick={addHandler}>addition</button>
应该是吗?
export const storeAction = counterSlice.actions