Redux - Register a Store Listener how to increment value of count

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;
const addone=()=>count+=1;
// Change code below this line
const a=()=>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);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36 Edg/103.0.1264.71

Challenge: Redux - Register a Store Listener

Link to the challenge:

First of all, please ask a question. A BIIIIIG part of being a developer is communication. On a job, there are A LOOOOT of times when you have a problem and you have to explain to someone what the problem is.

Your problems are here:

const addone=()=>count+=1;
const a=()=>store.subscribe(addOne());

First of all, what are you going to call that function “addone” or “addOne”? The second one is more inline with JS because it is camelCase, but they at least have to match.

Also, you’re just supposed to call store.subscribe, not wrap it in a function.

Also, what you’re passing store.subscribe… You are supposed to pass it the function, or a reference (memory address). The reference is stored in the variable. If you pass it addOne(), you are calling that function, so really what you are saying is “call that function (addOne) when you get to that line and whatever that returns, pass that to store.subscribe.” But addOne doesn’t return anything so really what you are saying is store.subscribe(undefined). You are supposed to pass it a function.

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