admin管理员组文章数量:1401171
I am new to Redux. I wonder what unsubscribe listener fundamentally is and how it works? I know that register function returns an unsubscribe but in the following example when we call the unsubscribe method why it does not just trigger a new function nested inside the variable? As we can see:
let unsubscribe = store.subscribe(() => {
// let unsubscribe nests a function
// execute every time the state changes
const state = store.getState();
});
// but here instead of call the nested function it cancels the listener, how is it possible ?
unsubscribe();
Thanks
I am new to Redux. I wonder what unsubscribe listener fundamentally is and how it works? I know that register function returns an unsubscribe but in the following example when we call the unsubscribe method why it does not just trigger a new function nested inside the variable? As we can see:
let unsubscribe = store.subscribe(() => {
// let unsubscribe nests a function
// execute every time the state changes
const state = store.getState();
});
// but here instead of call the nested function it cancels the listener, how is it possible ?
unsubscribe();
Thanks
Share Improve this question edited Aug 24, 2019 at 12:06 Yilmaz 50k18 gold badges218 silver badges271 bronze badges asked Jun 16, 2018 at 23:48 Diagathe JosuéDiagathe Josué 12.2k14 gold badges49 silver badges93 bronze badges3 Answers
Reset to default 6I think it is a little late to answer this question, but to be more clear, I would like to present it.
Key things to keep in mind are:
- What exactly is
store.subscribe
, take a demo of the youtubesubscribe
option (and bell icon), now whenever channel admin uploads a new video, it instantly calls listener(i.e. subscribe) here and gets you notified, now if you unsubscribe then you won't get notified. Simple enough! store.subscribe
or say the listener function is called whenever the state is changed because of the dispatched action.- return type of the subscribe function is again a function the
unsubscribes
the change listener.
//Action is an object with type property
const BUY_CAKE = 'BUY_CAKE';
// Action creator is a function that returns an actions
function buyCake() {
return {
type: BUY_CAKE,
info: 'first redux action',
}
}
// initial state for reducer, Reducer -> (previousstate, action) =>newState
const initialState = {
numOfCakes: 10
}
// This is our reducer function
const reducer = (state = initialState, action) => {
switch (action.type) {
case BUY_CAKE: return {
...state, // making the copy of state object
numOfCakes: state.numOfCakes - 1
}
default: return state;
}
}
const store = createStore(reducer);
console.log("initial state", store.getState());
/*Returns the current state tree of your application. It is equal to the last value returned by the store's reducer.*/
// The subscribe listerner will be called eveytime an action is dispatched
const unsubscribe = store.subscribe(() => console.log("updated state", store.getState()))
store.dispatch(buyCake());
store.dispatch(buyCake());
unsubscribe();
store.dispatch(buyCake());
console.log("state after unsubscribe", store.getState());
This will give the output
initial state { numOfCakes: 10 }
updated state { numOfCakes: 9 }
updated state { numOfCakes: 8 }
state after unsubscribe { numOfCakes: 7 }
So you see, after unsubscribing the listener is not called automatically. So here is the final catch
As you call the
unsubscribe
, it is a function which is returned from thesubscribe
function so it does not call the subscribe function again, rather call another function whichunsubscribes the change listener
.
the unsubscribe function in redux is actually implemented inside the subscribe method in redux createStore method. Here is how it works:
//Inside store
const subscribe = (listener) => {
listeners.push(listener);
return () => {
listeners.filter(l => l !== listener);
};
};
// End of inside store
const unsubscribe = store.subscribe(handleChange)
unsubscribe()
We are invoking store.subscribe() and passing it a callback function. That callback gets called whenever the store changes.
The function signature explains that subscribe expects a function that will be invoked on every dispatch and returns a function that when invoked will unsubscribe the listener. imagine like this:
function store.subscribe(callback){
//execute the callback function
callback();
return store.unsubscribe()
}
本文标签: javascriptReduxhow unsubscribe listener worksStack Overflow
版权声明:本文标题:javascript - Redux - how unsubscribe listener works? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744292071a2599163.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论