Hello,
I am in middle of this challenge:
And I got one question (or more like a basic concept that needs to be cleared).
This is what I came up with:
const ADD = 'ADD';
const reducer = (state = 0, action) => {
switch(action.type) {
case ADD:
return state + 1;
default:
return state;
}
};
const store = Redux.createStore(reducer);
// Global count variable:
let count = 0;
// Change code below this line
const increment = (count) => ++count; // passing "count" as an argument
store.subscribe(increment);
// Change code above this line
store.dispatch({type: ADD});
console.log(count);
store.dispatch({type: ADD});
console.log(count);
store.dispatch({type: ADD});
console.log(count);
And this is the answer:
// Change code below this line
const increment = () => ++count; // no argument "count" here
store.subscribe(increment);
// Change code above this line
The only difference between mine and the answer is the âcountâ I tried to pass as the argument.
Intuitively, it makes sense to me that we need âcountâ as the argument so that the function can do increment on it. However, the result does not support this idea.
To get a better understanding, I also tried codes below to see whatâs going on:
let count = 0;
let increment = (count) => ++count;
increment(count);
console.log(count) // 0
to compare to this case below trying to mimic the issue above:
let count = 0;
let increment = () => ++count;
increment();
console.log(count) // 1
Can someone tell me whatâs the correct concept behind this?
Why passing âcountâ as the argument doesnât work?
Thank you.