Redux - Register a Store Listener - BSWyJdIjagXrCk69mW_Sj

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;
    return state;

const store = Redux.createStore(reducer);

// Global count variable:
let count = 0;

// Change code below this line
const incrementCount =()=>{ store.getState() +1}
store.subscribe((incrementCount )=>{console.log('sub')})

// Change code above this line

store.dispatch({type: ADD});
store.dispatch({type: ADD});
store.dispatch({type: ADD});
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0

Challenge: Redux - Register a Store Listener

Link to the challenge:

First of all, at this point in your learning, you should be a doing a better job describing your problems. This is an important skill for a developer. That and being able to search online for answers.

But yeah, redux is weird at first. Don’t get frustrated.

The instructions was:

Write a callback function that increments the global variable count every time the store receives an action…

I think that is a poorly worded sentence, It should tell you to write a function that increments the global variable count, which is defined a few lines above.

const incrementCount =()=>{ store.getState() +1}

That does nothing with the global variable count. It also really doesn’t do anything - it retrieves the state (which it doesn’t need to do) and then adds 1 to it, and then does nothing with that value - it disappears into the mist.

You just need a function that increments count.


… and pass this function in to the store.subscribe() method.

You have this:

store.subscribe((incrementCount )=>{console.log('sub')})

You are passing it a function that logs out something (which it doesn’t need to do). You are assuming that subscribe is going to pass in a parameter called “incrementCount” - note that this is not the function you created - you think subscribe is going to pass something in there, you just gave it the same name.

But you don’t have to worry about anything like that - just pass your callback function to subscribe - it’s simpler than what you’re trying.

Try it again. If it doesn’t work, show us what you’re trying and we’ll get you closer.

1 Like

OK, I thought about it a little more. Yes, your callback function can just increment count itself. But I think the wording means that we want to use the store, like you did in your original callback. But we don’t need to do anything with it - it has already been incremented by the reducer - we just need to store it in our variable. Both approaches pass but I think the second is what they want.

It’s a confusing lesson and I’m not 100% sure why we’re doing this. But redux is confusing and is hard to teach, it’s hard to break into small chunks that make sense on their own.

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