Q
Q
Quintis2019-11-25 16:46:39
JavaScript
Quintis, 2019-11-25 16:46:39

How to write a simple counter in redux?

Good day, you need to write a simple counter to pass the FCC task - https://www.freecodecamp.org/learn/front-end-libra... I
wrote the code but it does not count, who can tell where the error is?

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.type) {
    case 'INCREMENT':
      return state+1;
      break;
    case 'DECREMENT':
      return state-1;
          break;
    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
console.log(store.dispatch(incAction()));

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2019-11-25
@Lynn

Your reducer always returns 0.
https://developer.mozilla.org/en/docs/Web/JavaScri...

If used postfix, with operator after operand (for example, x++), then it increments and returns the value before incrementing .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question