Redux - Write a Counter with Redux

Tell us what’s happening:

Describe your issue in detail here.

*Your code so far

Hi guys, it keeps saying I should Dispatching incAction on the Redux store should increment and decrement to the state by 1`javascript. I need help

const INCREMENT = "INCREMENT"; // Define a constant for increment action types
const DECREMENT = "DECREMENT"; // Define a constant for decrement action types

const counterReducer = (state = 0, action) => {
  switch(action.types){
    case INCREMENT:
    return state++;
    case DECREMENT:
    return state--;
    default:
    return state;

  }
}; // Define the counter reducer which will increment or decrement the state based on the action it receives

const incAction = () => {
  return {
    type: INCREMENT
  };
}; // Define an action creator for incrementing

const decAction = () => {
  return {
    type: DECREMENT
  };
}; // Define an action creator for decrementing

const store = Redux.createStore(counterReducer); // Define the Redux store here, passing in your reducers

Your browser information:

User Agent is: Mozilla/5.0 (iPhone; CPU iPhone OS 15_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Mobile/15E148 Safari/604.1

Challenge Information:

Redux - Write a Counter with Redux

You’re mutating the state variable and you are not returning anything useful.

The function accepts the state, plus an action, then returns a new version of the state. It shouldn’t mutate anything, this is critical. And whatever the function returns, that is the new state. So if it returns undefined, then that’s now your state (then if you try to increment that, everything will blow up).

In this case, the state is just a number. So to increment, you should be returning the number with one added to it.

Also, the action creators return an object with a type property, not types so it isn’t action.types but action.type


As said, you should not mutate the state. But if your increment/decrement was prefix ++var and not postfix var++ you would actually pass (but as said, don’t do that).

2 Likes

thanks, saw where i got it wrong

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.