umi-plugin-keep-alive
umi-plugin-keep-alive copied to clipboard
umi@3中,路由跳转似乎快于缓存的生成,导致缓存不生效
使用场景: 列表页缓存列表数据。跳转前设置when为true,然后通过histpry.push进行页面跳转
const [keepAlive, setKeepAlive] = useState(false);
const columns = [
{
title: 'xx',
render: ({id}) => <Button onClick={() => redirect(id)}>click</Button>
}
]
function redirect(id: number | string) {
setKeepAlive(true);
history.push(`consumer/${id}`);
}
return (
<KeepAlive saveScrollPosition="screen" when={keepAlive}>
<Table columns={columns} />
</KeepAlive>
)
问题:
缓存不生效,需要在setKeepAlive
后加一个延时(delay函数是setTimeout的包装),缓存才会工作,之前在umi@2里没有这个问题
async function showConsumerDetail(id: number | string) {
setKeepAlive(true);
await delay(0);
history.push(`consumer/${id}`);
}
有问题的版本:
"umi": "^3.2.0",
"umi-plugin-keep-alive": "^0.0.1-beta.16",
正常的版本:
"umi": "^2.13.13",
"umi-plugin-keep-alive": "^0.0.1-beta.6"
没看懂为啥要操作 when 属性?
没看懂为啥要操作 when 属性?
when为true才会开启缓存不是么,我这边只需要在指定的几个链接跳转之后缓存它,别的时候不需要
默认是 true ,你的需求也可以这样实现,会简单一些
<KeepAlive when={() => location.pathname === 你的路由地址}>
如果是动态路由的话可以考虑使用 router 自带的 match 方法
默认是 true ,你的需求也可以这样实现,会简单一些
<KeepAlive when={() => location.pathname === 你的路由地址}>
如果是动态路由的话可以考虑使用 router 自带的 match 方法
意思是从页面层级做缓存是吗,列表页很多的话我担心内存占用会是个问题,因为这个系统页面还是不少的 现在是只对列表组件做了封装,考虑是只在列表打开子页面之前缓存它,去别的页面就不管了
实现的功能和你现在想做的一致,仅缓存指定的路由,只是不需要通过 setKeepAlive 这样的方式
实现的功能和你现在想做的一致,仅缓存指定的路由,只是不需要通过 setKeepAlive 这样的方式
感谢解答,从页面的层级去做缓存确实可以解决现在的问题,不过页面跳转前对部分组件做精确缓存的场景还是有的,暂时不清楚这个问题到底是react-keepalive、umi-plugin-keep-alive还是umi造成的,如果这边没有想法的话,可以先关掉这个问题了