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

在已缓存的界面中、先调用dropByCacheKey手动清除缓存、在重新加载该页面缓存还是存在 #64

Closed
Myhappyboy opened this issue Nov 22, 2019 · 10 comments
Labels
enhancement New feature or request

Comments

@Myhappyboy
Copy link

作者你好:在多个页面已经缓存的情况下、需要做一个刷新页面功能。我尝试了调用dropByCacheKey方法、手动清除缓存的页面,然后在重新路由跳转到这个页面,结果是缓存还存在应该怎么处理?

@CJY0208
Copy link
Owner

CJY0208 commented Nov 22, 2019

是先离开目标路由,清空缓存后再回来,还是直接在目标路由上清空缓存呢?

@Myhappyboy
Copy link
Author

是直接在目标路由上清除缓存

@CJY0208
Copy link
Owner

CJY0208 commented Nov 22, 2019

按照目前的实现,这样是没法用 dropByCacheKey 清掉的,因为地址始终匹配当前路由,dropByCacheKey 将在内部工作中无效

目前如果要实现你的刷新功能,可以尝试变更组件 key 值强制触发组件重新渲染实现

猜测原始的配置大概如下

function RealContent() {
  return (
    ...
  )
}

<CacheRoute ... component={RealContent} />

尝试变更为如下

function RealContent() {
  return (
    ...
  )
}

function Wrapper(props) {
  const [renderKey, setRenderKey] = useState(Math.random())

  const refresh = () => {
    setRenderKey(Math.random()) // 变更 key 触发 RealContent 重渲染
  }

  return (
    <RealContent
      key={renderKey}  
      {...props} 
      refresh={refresh}
    />
  )
}

<CacheRoute ... component={Wrapper} />

如此,RealContent 内部如果需要刷新页面,可调用 props.refresh() 实现

后续考虑将此功能加入 dropByCacheKey 中,将可以在目标路由上直接清除缓存

@CJY0208 CJY0208 added the enhancement New feature or request label Nov 22, 2019
@Myhappyboy
Copy link
Author

好的!感谢提供的思路。

@Ray-56
Copy link

Ray-56 commented Dec 11, 2019

@Myhappyboy
我现在的思路是这样的创建一个 RedirectPage:

import React, { useEffect } from 'react';
import { RouteComponentProps } from 'react-router-dom';
import { LocationDescriptorObject } from 'history';
import { dropByCacheKey, getCachingComponents, getCachingKeys } from 'react-router-cache-route';

interface LocatioStateProps extends LocationDescriptorObject {}

interface RedirectProps extends RouteComponentProps<any, any, LocatioStateProps> {}

const Redirect: React.FC<RedirectProps> = (props) => {
    useEffect(() => {
        const cachingKeys = getCachingKeys();
        dropByCacheKey('Item');
        const cachingComponents = getCachingComponents();
        setTimeout(() => {
            props.history.replace(props.location.state);
        }, 0) 
    }, []);
    return null;
};

export default Redirect;

也符合作者现有的概念,带参进进入新页面,来进行重定向,先删除,然后跳转

@LiuL0703
Copy link

请问 先离开目标路由,调用dropByCacheKey清空缓存后再打开 缓存还在怎么处理 @CJY0208

@CJY0208
Copy link
Owner

CJY0208 commented Dec 17, 2019

请问 先离开目标路由,调用dropByCacheKey清空缓存后再打开 缓存还在怎么处理 @CJY0208

不重新打开目标路由,用 devtools 看到是清空成功了吗?可能是清空不成功

@LiuL0703
Copy link

LiuL0703 commented Dec 17, 2019

确实是清空不成功 但是debug进去是走了run(component,reset) @CJY0208

发现是在当前路由下清空不成功 非当前路由下可以清空

嵌套路由不管是在当前路由下还是非当前路由下都无法清空

@CJY0208
Copy link
Owner

CJY0208 commented Dec 18, 2019

嵌套路由的话,一般是父路由优先缓存,然后冻结了其内部状态,所以子路由(目标路由)实际没有缓存,这是个问题,目前尚未解决,最早在 #46 提出

@CJY0208
Copy link
Owner

CJY0208 commented Sep 19, 2020

v1.10.0 中新增了 refreshByCacheKey 功能,可实现在已缓存状态下刷新缓存的功能

import CacheRoute, { refreshByCacheKey } from 'react-router-cache-route'

<CacheRoute ... cacheKey="MyComponent" />

refreshByCacheKey('MyComponent') // to refresh

感谢 @xwinstone

@CJY0208 CJY0208 closed this as completed Sep 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants