Write a Counter with Redux - lost

Write a Counter with Redux - lost
0

Tell us what’s happening:
I m getting the following errors, basically saying the program isn’t working as expected

" The Redux store should initialize with a state of 0.

Dispatching incAction on the Redux store should increment the state by 1.

Dispatching decAction on the Redux store should decrement the state by 1.
"

But i tested it using the console.log statements at the end of the code, and it shows the state has been initialized to 0, incrementing and decrementing correctly.

0
2
1

I m lost at what the lessons is expecting.

Your code so far


const INCREMENT = 'AT_INCREMENT'; // define a constant for increment action types
const DECREMENT = 'AT_DECREMENT'; // define a constant for decrement action types

const counterReducer = (state={ count:0 }, action) => {
    let result = state.count;
    switch(action.type) {
        case INCREMENT:
            result++;
            return { 
                type:INCREMENT,
                count:result
            };
        case DECREMENT:  
            result--;
            return { 
                type:DECREMENT,
                count:result
            };
        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.getState().count);
store.dispatch(incAction());
store.dispatch(incAction());
console.log(store.getState().count);
store.dispatch(decAction());
console.log(store.getState().count);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0.

Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/redux/write-a-counter-with-redux

Good try! Now here are few things to notice.

The Redux store should initialize with a state of 0.

You are setting initial state to an object with key of 0. It should just be state=0. Next there is no need to return an object with type and count. You just want to return count, specifically a number.

Hope this helped shed some light!

I tried changing the state to just a number as follows, but I still get this error below. What is the right way to initialize a state?

The Redux store should initialize with a state of 0.

const counterReducer = (state=0, action) => {
let result = state;
switch(action.type) {
case INCREMENT:
result++;
return result;
case DECREMENT:
result–;
return result;
default:
return state;
}
};

Your code is passing for me. Make sure it’s result-- not result-.

Also remove your console.logs and dispatches at the bottom.

Works after removing the test code at the bottom, thanks.

Thank you, you saved my life. I was stuck with the same problem for an hour and as you suggested, i removed my console.logs then the problem solved. However, I don’t know what’s happening here. How did the console.log intervene in the testing?

I think the tests rely on the count of these action calls. As you console log it you inadvertently call those functions. This is my theory :slight_smile: