小编Jae*_*eSo的帖子

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;

Run Code Online (Sandbox Code Playgroud)

reactjs react-native

3
推荐指数
1
解决办法
1249
查看次数

无法使用 npm 更新 React 和 React Native

我正在尝试更新到react@17.0.1和react-native@0.64.0-rc.0,但遇到以下错误:

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: undefined@undefined
npm ERR! Found: react@16.13.1
npm ERR! node_modules/react
npm ERR!   react@"16.13.1" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer react@"17.0.1" from react-native@0.64.0-rc.0
npm ERR! node_modules/react-native
npm ERR!   react-native@"0.64.0-rc.0" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to …
Run Code Online (Sandbox Code Playgroud)

npm react-native

3
推荐指数
1
解决办法
1457
查看次数

标签 统计

react-native ×2

npm ×1

reactjs ×1