【问题标题】:Cleanup ref issues in ReactReact 中的清理引用问题
【发布时间】:2021-07-08 05:02:06
【问题描述】:

我有一个问题,因为 ESLINT 在控制台中输出错误。我想解决控制台中的问题。 请在此处检查代码和框

CLICK HERE

更新问题

The 'callbackFunction' function makes the dependencies of useEffect Hook (at line 33) change on every render. Move it inside the useEffect callback. Alternatively, wrap the definition of 'callbackFunction' in its own useCallback() Hook. (react-hooks/exhaustive-deps)

老问题

The ref value 'containerRef.current' will likely have changed by the time this effect cleanup function runs. If this ref points to a node rendered by React, copy 'containerRef.current' to a variable inside the effect, and use that variable in the cleanup function. (react-hooks/exhaustive-deps)
eslint

React Hook useEffect has a missing dependency: 'callbackFunction'. Either include it or remove the dependency array. (react-hooks/exhaustive-deps)

代码

  useEffect(() => {
    const observer = new IntersectionObserver(callbackFunction, options);
    if (containerRef.current) observer.observe(containerRef.current);

    return () => {
      if (containerRef.current) observer.unobserve(containerRef.current);
    };
  }, [containerRef, options]);

【问题讨论】:

    标签: reactjs react-hooks eslint


    【解决方案1】:

    ref 值containerRef.current 可能已被 此效果清理功能运行的时间。如果这个 ref 指向一个节点 由 React 渲染,复制 containerRef.current 到里面的一个变量 效果,并在清理函数中使用该变量。 (react-hooks/exhaustive-deps)

    只需将当前 ref 值保存到一个局部范围的变量中,以便在效果的清理函数中关闭。

    React Hook useEffect 缺少一个依赖项:callbackFunction。 包括它或删除依赖数组。 (react-hooks/exhaustive-deps)

    callbackFunction 值更新时,您将需要清理所有旧订阅的观察者、引用、回调等……如果有的话。将其添加到依赖数组中。

    useEffect(() => {
      let observerRefValue = null; // <-- variable to hold ref value
    
      const observer = new IntersectionObserver(callbackFunction, options);
    
      if (containerRef.current) {
        observer.observe(containerRef.current);
        observerRefValue = containerRef.current; // <-- save ref value
      }
    
      return () => {
        if (observerRefValue) observer.unobserve(observerRefValue); // <-- use saved value
      };
    }, [callbackFunction, containerRef, options]);
    

    更新

    'callbackFunction'函数使useEffect的依赖 Hook(在第 33 行)在每次渲染时更改。将它移到 useEffect 中 打回来。或者,将“callbackFunction”的定义包装在 它自己的 useCallback() Hook。 (react-hooks/exhaustive-deps)

    您可以通过包装在 useCallback 钩子中来记住此回调:

    const callbackFunction = React.useCallback((entries) => {
      const [entry] = entries;
      onIntersection(video.id, entry);
    }, [onIntersection, video]);
    

    或者您可以简单地将回调移入 useEffect 挂钩并更新依赖项:

    useEffect(() => {
      const callbackFunction = (entries) => {
        const [entry] = entries;
        onIntersection(video.id, entry);
      };
    
      let observerRefValue = null;
    
      const observer = new IntersectionObserver(callbackFunction, options);
    
      if (containerRef.current) {
        observer.observe(containerRef.current);
        observerRefValue = containerRef.current;
      }
    
      return () => {
        if (observerRefValue) observer.unobserve(observerRefValue);
      };
    }, [containerRef, onIntersection, options, video]);
    

    【讨论】:

    • 谢谢。现在我在callbackFunction 的codesandbox 中有另一个错误。你能检查一下吗?谢谢
    • @Joseph 当然,更新了答案和建议。
    • @Joseph 听起来您可能正在使用next.js (?)。症状听起来像是过时的状态。如果下一页在同一路由/路径上,那么我的猜测是它不会对指示页面已更改的任何路由参数的更新做出反应。解决方案是使用componentDidUpdate(基于类的组件)或另一个具有适当依赖关系的useEffect钩子来更新您可能用于呈现内容的任何本地状态。
    • @Joseph 您可以分叉您的沙箱并尝试转换为 Next.js 模板,然后添加任何必要的代码来生成重现问题的运行沙箱工件。全面披露:我可以帮助调试正在运行的 Next.js 应用程序,但我没有 0 经验站立。我建议努力创建一个新的沙箱,并在此处使用适当的标签在 SO 上提出 new 问题,您可能会更快地获得更好的帮助。请随时向我提供问题链接,我可以看看。
    • @Joseph 同时,如果您的问题已通过答案得到充分解决,那么我邀请您将其标记为已接受,如果有帮助,请不要忘记也投票。干杯。
    【解决方案2】:

    参考您的更新问题,您需要使用 useCallback

      const callbackFunction = useCallback((entries) => {
        const [entry] = entries;
        onIntersection(video.id, entry);
      }, [video, onIntersection]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 2021-03-29
      • 1970-01-01
      • 2021-05-27
      • 2016-09-08
      • 2022-11-24
      • 1970-01-01
      相关资源
      最近更新 更多