react-activation icon indicating copy to clipboard operation
react-activation copied to clipboard

关于AliveScopes 缓存与 keepAlive unmount之间的时序问题?

Open DavosLi opened this issue 3 years ago • 3 comments

先列举一下根据代码总结的处理流程: 1.KeepAlive mount阶段

  • 1.组件A使用 KeepAlive 缓存
  • 2.KeepAlive 首次渲染,Constructor 中 init 函数初始化,调用 props._helpers.keep 缓存组件
  • 3.helper.keep处理缓存并触发AliveScopes render, 只有当Keeper mount之后,才会在AliveScope的store中添加keeper实例的cache对象

2.KeepAlive unmount阶段

  • 1.KeepAlive unMount时,根据id将store中对应的cache.cached置为true
  • 2.drop/dropScope 清理缓存时,需要检查cached是否为true

问题出mount阶段的 2 和 3 之间,由于2和3之间时异步的(helper中函数使用Promise? React异步渲染?),如果中途KeepAlive就unmount,此时在store中还没缓存,所以cache.cached无法标记为true,之后就无法调用drop/dropScope清理缓存节点.

上述是项目最近遇到的一个问题的分析,不知道我的理解对不对,如果理解没有问题的话,请问这种情况如果解决呢?

DavosLi avatar Jun 24 '21 03:06 DavosLi

有这个可能,目前的现象是挂载后立即卸载,卸载不掉吗?

CJY0208 avatar Jun 25 '21 01:06 CJY0208

有这个可能,目前的现象是挂载后立即卸载,卸载不掉吗?

你指的是无法卸载已经缓存的组件吗?那是的,出现上述问题时,后面无法使用drop/dropScope卸载已缓存的组件

DavosLi avatar Jun 25 '21 03:06 DavosLi

请问如何解决,我也遇到这个问题了

MilkSunshine avatar Nov 19 '21 19:11 MilkSunshine