我正在 React 上测试 Redux,当我在控制台中运行 npm run start 时,我收到了提到的错误消息,这是我的代码,在 index.js 文件中,使用“npx create-react-app”生成:
import {createStore} from 'redux';
const increment = ()=>{
return{
type:'INCREMENT'
}
}
const decrement =()=>{
return {
type:'DECREMENT'
}
}
const counter = (state = 0,action)=>{
switch(action.type){
case "INCREMENT":
return state + 1;
case "DECREMENT":
return state - 1;
}
}
let store = createStore(counter);
store.subscribe(()=>{
console.log(store.getState())
})
store.dispatch(increment);
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
我做错了什么?
最佳答案
您正在将一个函数分派(dispatch)到商店,而它应该是一个对象。尝试使用这个 store.dispatch(increment())
关于javascript - Redux 操作错误必须是普通对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63827374/