Reactjs 卸载组件时,useSelector是否会自动从存储更新中取消订阅该组件?

Reactjs 卸载组件时,useSelector是否会自动从存储更新中取消订阅该组件?,reactjs,redux,react-redux,Reactjs,Redux,React Redux,作为react redux库的一部分,我正在查看useSelector的源代码 我想知道当组件卸载时,useSelector是否会自动取消订阅redux store更新中的组件,类似于在redux中调用store.subscribe返回的unsubscribe函数?或者,如果另一个组件更改了该状态,该组件是否仍将订阅此类更改?是,useSelector在组件卸载时显式取消订阅存储。Per: 使用同构布局效果(()=>{ 函数checkForUpdates(){ 试一试{ const newSel

作为react redux库的一部分,我正在查看
useSelector
的源代码


我想知道当组件卸载时,
useSelector
是否会自动取消订阅redux store更新中的组件,类似于在redux中调用
store.subscribe
返回的
unsubscribe
函数?或者,如果另一个组件更改了该状态,该组件是否仍将订阅此类更改?

是,
useSelector
在组件卸载时显式取消订阅存储。Per:

使用同构布局效果(()=>{
函数checkForUpdates(){
试一试{
const newSelectedState=latestSelector.current(store.getState())
if(equalityFn(newSelectedState,latestSelectedState.current)){
返回
}
latestSelectedState.current=newSelectedState
}捕捉(错误){
//我们在这里忽略所有错误,因为组件
//重新呈现时,将再次调用选择器,并且
//如果既没有道具也没有存储状态,将再次抛出
//改变
latestSubscriptionCallbackError.current=err
}
forceRender()
}
subscription.onStateChange=检查更新
subscription.trysubscripte()
checkForUpdates()
return()=>subscription.tryunsubscripte()//在此处取消订阅
},[商店,订阅])

是的,当创建新状态时,将不再调用传递给useSelector的回调,并且当组件卸载时,react-redux不会对组件进行任何更新。