📜  在 React 中删除未安装组件上的 setState 警告 - 无论代码示例

📅  最后修改于: 2022-03-11 14:58:17.285000             🧑  作者: Mango

代码示例2
function useAsync(asyncFn, onSuccess) {
  useEffect(() => {
    let isActive = true;
    asyncFn().then(data => {
      if (isActive) onSuccess(data);
    });
    return () => { isActive = false };
  }, [asyncFn, onSuccess]);
}