I have looked at other solutions. I really wanted to make use of Object.assign property as mentioned in the redux documentation. What I am trying to do is make a shallow copy of the default state object and return a new state that will update the number property to the new value.
Wow, @zootechdrum your code seems to be more mature than mine.
Took me a while to solve it the way I did.
But some times, I just feel like Freecodecamps Console is very weird. lol.
Here’s my code, which I used to pass the challenge.
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
While I was using the unary operators (state++ and state–) for changing the state, I wasn’t getting it.
Anyways. Its all good.
I just admire how you solved the problem with your code, especially within the switch statement.
Oh my gosh! I was having the same problem and I was using the unary operators for incrementing and decrementing also! But it must have been because I was putting them in the wrong place. I tried prefixing them to return ++state and --state and it passed. It’s gotta be because return state++ will return the value of state before it’s incremented, making it look like it’s not being incremented at all.
Thank you so much! Your feedback led me to figuring that out.