irychen / keepalive-for-react

React KeepAlive is a component that can cache the state of the component and reuse it when needed.
MIT License
162 stars 32 forks source link

update: Compatible with SWR data request library #18

Closed ybf970928 closed 1 month ago

ybf970928 commented 1 month ago

说明: 当项目当中使用到了类似SWR或者react-query的请求库时, 由于请求库代码内部通过 useLayoutEffect 进行key的验证, 但是优先级低于KeepAlive 里的 setCacheNodes, 所以会出现 A页面切换到B页面后, A页面接口又被错误的调用一次

irychen commented 1 month ago

需要兼容一下React 16.8

import { startTransition as reactStartTransition } from 'react';

export const safeStartTransition = (callback: () => void) => {
  if (typeof reactStartTransition === 'function') {
    reactStartTransition(callback);
  } else {
    // 如果 startTransition 不可用,直接执行回调
    callback();
  }
};
ybf970928 commented 4 weeks ago

需要兼容一下React 16.8

import { startTransition as reactStartTransition } from 'react';

export const safeStartTransition = (callback: () => void) => {
  if (typeof reactStartTransition === 'function') {
    reactStartTransition(callback);
  } else {
    // 如果 startTransition 不可用,直接执行回调
    callback();
  }
};

突然发现, 好像顺带着也把antd的下拉框回显时机错误之类的问题也解决掉了😂, 不在需要在onBeforeActive内处理

irychen commented 4 weeks ago

very nice!