reactjs - React useCallback 内存泄漏卸载组件
问题描述
我是 React 的新手,我收到了这个错误:
警告:无法对未安装的组件执行 React 状态更新。这是一个空操作,但它表明您的应用程序中存在内存泄漏。要解决此问题,请在 useEffect 清理函数中取消所有订阅和异步任务。
据我了解,似乎我有内存泄漏并且需要对我的 useCallback 挂钩进行 useEffect 清理?
我尝试添加一个useRef
来检查挂载,但返回不会将状态更改为 false。
const MyComponent = ({data}) => {
const mounted = useRef(true);
const [loading, setLoading] = useState(false);
const isLoading = useCallback(async () => {
setLoading(true);
if (data) {
console.log('YAY DATA: ', data);
}
return () => {
setLoading(false); // unreachable code
mounted.current = false; // does this do the cleanup?
};
}, [loading]);
return (
//...some component
);
};
export default MyComponent;
解决方案
如果您执行的异步操作在完成后需要为状态设置某些内容,则必须确保在更新状态之前组件仍处于挂载状态。
为了做到这一点,你尝试这样的事情:
const MyComponent = (props) => {
const mounted = useRef(false);
useEffect(() => {
mounted.current = true; // Will set it to true on mount ...
return () => { mounted.current = false; }; // ... and to false on unmount
}, []);
const [loading, setLoading] = useState(false);
const [data, setData] = useState([]);
const myAsyncCallback = useCallback(async () => {
setLoading(true);
// Await for an async function...
const data = await yourAsyncFunction(...); // Complete with proper code here
// While waiting for yourAsyncFunction to be done,
// It's possible that your component had been unmounted.
// Therefore, you have to check if the component is still mounted before updating states
if (mounted.current) {
setLoading(false);
setData(data);
}
}, []);
return (
//...some component that calls myAsyncCallback
);
};
export default MyComponent;
推荐阅读
- java - 无法使用 spring-data-ldap 从 AD 获取自定义属性
- amazon-web-services - 53 路由加权策略的健康检查失败时会发生什么?
- java - ClassNotFoundException: com.mysql.jdbc.Driver 使用 Java Mysql
- c# - 没有设置 UseSubmitBehavior="false" 的 ASP.Net 按钮 OnClick 不会启动
- c# - .NET 中的线程安全令牌处理
- spring-boot - @Repository 类没有在我的测试类中自动装配
- python - 对 FastAPI 的 multipart/form-data 请求给出 422 响应
- c# - 如何将数据库中的整数转换为文本框中的字符串?
- excel - 具有日期条件的自动过滤器返回空白
- typescript - 使用 Typescript 运行 babel 时出现意外的令牌