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

在umi的layout中使用keepalive包裹outlet的疑问 #299

Open
jxccc1998 opened this issue Dec 1, 2023 · 6 comments
Open

在umi的layout中使用keepalive包裹outlet的疑问 #299

jxccc1998 opened this issue Dec 1, 2023 · 6 comments

Comments

@jxccc1998
Copy link

No description provided.

@jxccc1998
Copy link
Author

jxccc1998 commented Dec 1, 2023

https://stackblitz.com/edit/react-ts-acqxdi?file=Page.tsx,Layout.tsx,index.tsx
在这个示例中,使用了useLocation更新key,但是每次切換路由会导致layout组件执行,使得layout上的公用组件会刷新,但是复用的layout在默认情况下应该是不会重新执行的。
但是好像其他办法绑定key会使得缓存无效,不确定是什么原因导致的(似乎只有useLocation可以和路由同步更新)

@jxccc1998 jxccc1998 changed the title 在类 在umi的layout中使用keepalive包裹outlet的疑问 Dec 1, 2023
@MatthewTt
Copy link

请问你解决了吗

@jxccc1998
Copy link
Author

请问你解决了吗

暂时先把缓存变为组件层级,用hoc包裹就行。outlet内部应该是有变化的

@MatthewTt
Copy link

请问你解决了吗

暂时先把缓存变为组件层级,用hoc包裹就行。outlet内部应该是有变化的

能贴一下代码不,刚开始学习不太明白😂

@jxccc1998
Copy link
Author

请问你解决了吗

暂时先把缓存变为组件层级,用hoc包裹就行。outlet内部应该是有变化的

能贴一下代码不,刚开始学习不太明白😂

import { KeepAlive } from '@umijs/max';

const WithKeepAlive = (Component: any, name?: string) => {
  return function AddKeepAliveComponent() {
    return !!name ? (
      <KeepAlive cacheKey={name} name={name}>
        <Component />
      </KeepAlive>
    ) : (
      <Component />
    );
  };
};

export default WithKeepAlive;

在你要用的组件(页面级别的, Test是原来封装的组件)
export default WithKeepAlive(Test, location.pathname);

@MatthewTt
Copy link

请问你解决了吗

暂时先把缓存变为组件层级,用hoc包裹就行。outlet内部应该是有变化的

能贴一下代码不,刚开始学习不太明白😂

import { KeepAlive } from '@umijs/max';

const WithKeepAlive = (Component: any, name?: string) => {
  return function AddKeepAliveComponent() {
    return !!name ? (
      <KeepAlive cacheKey={name} name={name}>
        <Component />
      </KeepAlive>
    ) : (
      <Component />
    );
  };
};

export default WithKeepAlive;

在你要用的组件(页面级别的, Test是原来封装的组件) export default WithKeepAlive(Test, location.pathname);

感谢大哥🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants