Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tab 切换的时候组件卸载,导致状态丢失 #4839

Closed
lujun2 opened this issue Feb 24, 2022 · 6 comments
Closed

Tab 切换的时候组件卸载,导致状态丢失 #4839

lujun2 opened this issue Feb 24, 2022 · 6 comments
Labels

Comments

@lujun2
Copy link

lujun2 commented Feb 24, 2022

Version of antd-mobile

5.1.0

Operating system and its version

iOS

Browser and its version

Chrome 98.0.4758.80

Sandbox to reproduce

https://codesandbox.io/s/eager-mclean-k1pgc7?file=/src/App.tsx

What happened?

切换 Tab 的时候,Tab Content 渲染内容没有被保留
导致状态丢失,组件重新绘制


正常情况应该是第一次显示 Tab 时渲染内容,切换 Tab 进行只是不可见,DOM仍然需要保留
切换回来的时候将第一次生成的 DOM 重新显示出来

https://codesandbox.io/s/lively-worker-vifynu?file=/index.js

这个是antd的实现,切换 Tab 后 DOM 内容仍然保留

Relevant log output

No response

@lujun2 lujun2 added the bug label Feb 24, 2022
@zzzgydi
Copy link
Contributor

zzzgydi commented Feb 25, 2022

Tabs.Tab 可以加个 forceRender: true 参数

@lujun2
Copy link
Author

lujun2 commented Feb 25, 2022

Tabs.Tab 可以加个 forceRender: true 参数

这个可以达到效果


但我们这边的实际场景是 tab 里面是一个比较耗时的操作 (React PDF)
forceRender 会导致在一开始就全部渲染,界面比较卡顿

@zzzgydi
Copy link
Contributor

zzzgydi commented Feb 25, 2022

这个场景的话,可以先用forceRender保持子组件的渲染,然后在业务逻辑里对耗时操作进行类似懒加载(延迟执行?按需执行?)的操作。或者不用forceRender的话,就把状态丢到全局状态管理器里吧,或者状态提升也行。

@awmleer
Copy link
Member

awmleer commented Feb 25, 2022

的确复现出来了,我来看下

@awmleer
Copy link
Member

awmleer commented Feb 25, 2022

看起来需要对 Tabs 的实现逻辑做比较大的重构

@awmleer
Copy link
Member

awmleer commented Feb 28, 2022

已修复 5a9d665

@awmleer awmleer closed this as completed Feb 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants