Tell us what’s happening:
Describe your issue in detail here.
Your code so far
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 addOne = () => (count += 1);
function sayHi() {
console.log("Hi");
}
store.subscribe(sayHi)
// 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);
Your browser information:
User Agent is: Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1
Challenge: Redux - Register a Store Listener
Link to the challenge:
https://www.freecodecamp.org/learn/front-end-development-libraries/redux/register-a-store-listener don’t know what’s wrong with my code
Why are you passing sayHi
to subscribe
?
The callback to store.subscribe
should also increment the global count
variable as the store is updated.
Probably saw the following in the guide for this challenge:
How do you pass a function to a method?
We can pass a function to a method the same way we might pass a variable to a method. Just pass it in as an argument!
function sayHi() {
console.log("Hi!");
}
store.subscribe(sayHi);
I just updated the hint page with two solutions as I have seen this happen before. I only added the solution part and not the full code, but I can edit it again if that is really needed.
Historically, the solutions have full code. Or at least that is how things use to be done.
I know, but you would think the code comments should make it clear what part of the code it is for.
Also, if people really blindly copy and paste that and replace everything else I wonder how they ever managed to get to this part of the curriculum. Maybe catching such blind copy-pasting would really be for the best as it would point to some serious lack of understanding.
But I updated the solutions to be the complete code.
1 Like