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

第一个issue就是切实的干货,react如何支持类似 vue keep-alive的功能 #1

Open
robinson90 opened this issue Dec 5, 2018 · 8 comments

Comments

@robinson90
Copy link

@robinson90 robinson90 commented Dec 5, 2018

如题,基本用react开发入门阶段的同学,在使用列表组件然后进入详情并返回,或者其他类似场景时,很羡慕vue有直接的缓存特性。

那么react是如何解决这个问题的呢?

@robinson90

This comment has been minimized.

Copy link
Author

@robinson90 robinson90 commented Dec 5, 2018

首先提供的是官方关于这个问题的issue :facebook/react#12039

@robinson90

This comment has been minimized.

Copy link
Author

@robinson90 robinson90 commented Dec 5, 2018

image
image

从react作者大佬的观点来看,react本身不会支持这样的特性,你可以通过两种方式实现这样的需求:

基于组件设计

将其数据的保存提高到一个父级组件,不会因为生命周期影响。或者用redux,mobx的方案将其存储。在大佬的观点里,页面的状态都可以用数据来记录。

基于样式控制

在我们常规的思考中,都是页面跳转组件销毁来实现组件的切换,其实你可以简单的通过组件的显示隐藏来控制组件的展示,这样这个保存问题本来就不存在,因为组件没有进入到销毁周期。

@yexiuqing

This comment has been minimized.

Copy link

@yexiuqing yexiuqing commented Dec 7, 2018

目前正在做的都是内部系统,基本为表单操作,退出当前页面再返回时需要回填所有上次查询参数以及结果,用的也是redux,

@robinson90

This comment has been minimized.

Copy link
Author

@robinson90 robinson90 commented Dec 7, 2018

@yexiuqing 优秀

@CJY0208

This comment has been minimized.

Copy link

@CJY0208 CJY0208 commented Jan 23, 2019

官方没方案,不过可以依附于 react-router 给出一个临时工方案

https://github.com/CJY0208/react-router-cache-route

@gaoqiang19514

This comment has been minimized.

Copy link

@gaoqiang19514 gaoqiang19514 commented Apr 20, 2019

确实这个需求完全可以从设计的层面规避掉,而且会更棒

@liyaxiaoniu

This comment has been minimized.

Copy link

@liyaxiaoniu liyaxiaoniu commented Apr 25, 2019

可恶的物理键

@AielloChan

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can’t perform that action at this time.