Search code examples
javascriptreactjsreduxflux

Error on Redux Actions must be plain objects


I'm testing Redux on React, when I run npm run start in the console, I get the mentioned error message, this is my code, on index.js file, generated with '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')
);

what am I doing wrong ?


Solution

  • You are dispatching a function to the store where as it should be an object. Try using this store.dispatch(increment())