Register a store listener ( help)

Tell us what’s happening:
Describe your issue in detail here.
Tell me how to correct function store.subscribe() in bellow task,

Many thanks

  **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);
// 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 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.1 Safari/605.1.15

Challenge: Register a Store Listener

Link to the challenge:

The callback function inside the subscribe method should be the function that increment the count variable. It is the method that you pass inside the subscribe method that gets called whenever the state changes.
In your case it should be

store.subscribe(addOne);

Another thing you need to note is that you should initialize the count variable as 0 instead of 1, otherwise you will see that the values that gets logged to the console will always be incremented by one.

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
function addOne (){
console.log (“count”);
}
store.subscribe(addOne);
// 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);

Still not worked , would you give me any suggestion

The task is to change the value of the count variable so you have to change it’s value. You need not do console.log("count"), you may if you want though. But to pass the tests, you need to change the value of count in that function

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