Answer the question
In order to leave comments, you need to log in
How to unsubscribe from a store in Redux?
Greetings!
I'm learning Redux , I can't unsubscribe from the store. I unsubscribe from the store, then I make changes to the store in order to check whether I unsubscribed or not (did not unsubscribe).
Please explain how to unsubscribe from the store?
const store = createStore(reducer);
const INCREMENT = "INCREMENT";
const DECREMENT = "DECREMENT";
const RESET = "RESET";
const initialState = {count: 0};
function reducer(state = initialState, action) {
switch(action.type) {
case INCREMENT:
return {count: state.count + 1};
case DECREMENT:
return {count: state.count - 1};
case RESET:
return {count: 0};
default:
return state;
}
}
store.subscribe(() => console.log(store.getState()));
const unsubscribe = store.subscribe(() => console.log(store.getState()));
unsubscribe(); // отписались
// почему в консоль пишется инфа о изменение store, если ранее мы уже отписались?
store.dispatch({type: INCREMENT});
store.dispatch({type: INCREMENT});
store.dispatch({type: DECREMENT});
store.dispatch({type: RESET});
Answer the question
In order to leave comments, you need to log in
So you call subscrube twice, and call unsubscribe only for the second subscription.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question