Redux - Register a Store Listener

Tell us what’s happening:
I’m trying to clear the “The callback to store.subscribe should also increment the global count variable as the store is updated.” test. But I do not know what to do 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 = 1;
const addOne = () => (count += 1);
// Change code below this line
function sayHi() {
  console.log("Hi!");
}
store.subscribe((sayHi) => { ++count } );
// 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/106.0.0.0 Safari/537.36 Edg/106.0.1370.42

Challenge: Redux - Register a Store Listener

Link to the challenge:

you need to implement a function that will increment you count in the store.subscribe method.

store.subscribe ( // write a function that will increment your count);

Like you AddOne function.

Do you mean like this?

yes, but you don’t need the sayHi, anonymous function works

store.subscribe( () => ++count)

Ok, and it still not clearing the test.

You should see 1 2 3 in the console. Reset the code and just add

store.subscribe( () => count++)

where it is needed.

Ah, ok. Thank you for the info.

They’re basically asking you to put a function inside the store.subscribe() method. You could put the function directly inside like you just did, or call the function earlier

const addOne = () => (count += 1);

And put it inside the store.subscribe() method

store.subscribe(addOne)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.