Don't understand Redux store listener

Tell us what’s happening:
I’m going through the redux section of the front end libraries certification and I am so confused at what it wants me to do here to make count go up when the store is updated. It says to make a callback function but the example they give requires a static variable whereas this problem deals with a function being called / a value changing so it gave me 0 clarification. I can think of how to do it in react but im completely lost with what to put within my function which I’ve aptly titled “idk”.

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
let idk = function(){

}
store.subscribe(idk) 
// 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36.

Challenge: Register a Store Listener

Link to the challenge:

You just increment the count variable. It’s not a trick question – this is nothing to do with React or anything like that, it’s just asking you to add one to a number. You have a variable count. Write a function that adds one to count every time it gets called. The subscribe method on the Redux store object just lets you register a function that will be called every time the store updates.

1 Like

I was thinking the function had to reference a specific action taking place, didn’t realize that part was automatically within the .subscribe method. Thanks!