Tell us what’s happening:
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
store.subscribe(count+1);
// 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.61 Safari/537.36
.
Challenge: Register a Store Listener
Link to the challenge:
You should pass a function to store.subscribe();
1 Like
Normally, the redux working process is as follows:
- An action is dispatched to the store.
- The store received the action and send it to the reducer.
- The reducer updates the state in the store.
- The store notifies subscribers that the state is changed by calling the callback function provided by the subscriber.
- The subscriber updates the UI.
Callback function is simply a function for receivers to call it back later.
example 1:
store.subscribe(() => console.log(“something change is the store.”))
example 2:
function x(){
console.log(“something change is the store.”);
}
store.subscribe(x);
example 3:
store.subscribe(function(){ console.log(“something change is the store.”); });
1 Like