简体   繁体   English

如何解决此警告:“React Hook useEffect 缺少依赖项:'history'”?

[英]How to fix this warning: “React Hook useEffect has a missing dependency: 'history'”?

When I use the ScrollToTop component in my React app I get this warning in the browser:当我在我的 React 应用程序中使用 ScrollToTop 组件时,我在浏览器中收到以下警告:

Line 12:6: React Hook useEffect has a missing dependency: 'history'.第 12:6 行:React Hook useEffect 缺少依赖项:'history'。 Either include it or remove the dependency array react-hooks/exhaustive-deps要么包含它,要么移除依赖数组 react-hooks/exhaustive-deps

import { useEffect } from 'react';
import { withRouter } from 'react-router-dom';

function ScrollToTop({ history }) {
  useEffect(() => {
    const unlisten = history.listen(() => {
      window.scrollTo(0, 0);
    });
    return () => {
      unlisten();
    }
  }, []);

  return (null);
}

export default withRouter(ScrollToTop);

What change can I make to remove this warning?我可以进行哪些更改来删除此警告? Thanks!谢谢!

You can add history to dependency list of the useEffect() block:您可以将history添加到useEffect()块的依赖项列表中:

useEffect(() => {
  const unlisten = history.listen(() => {
    window.scrollTo(0, 0);
  });
  return () => {
    unlisten();
  }
}, [history]); // <- add history here

Or you can import history instead of passing it as a prop, and then avoid the need to include it in the useEffect() dependencies.或者您可以导入history而不是将其作为道具传递,然后避免将其包含在useEffect()依赖项中。

import { useEffect } from 'react';
import { withRouter } from 'react-router-dom';

function ScrollToTop({ history }) {
  useEffect(() => {
    const unlisten = history.listen(() => {
      window.scrollTo(0, 0);
    });
    return () => {
      unlisten();
    }
  }, [history]);

  return (null);
}

export default withRouter(ScrollToTop);

Add history to the array that is the second parameter of the useEffect hook function.history添加到作为useEffect挂钩 function 的第二个参数的数组中。 This is so that whenever history changes, unlisten will be called.这样一来,每当history发生变化时,都会调用unlisten

import { useEffect } from 'react';
import { withRouter } from 'react-router-dom';

function ScrollToTop({ history }) {
  useEffect(() => {
    const unlisten = history.listen(() => {
      window.scrollTo(0, 0);
    });
    return () => {
      unlisten();
    }
  }, [history]); // <= you need this

  return (null);
}

export default withRouter(ScrollToTop);

API: https://reactjs.org/docs/hooks-effect.html Example: API: https://reactjs.org/docs/hooks-effect.html示例:

useEffect(() => {
  document.title = `You clicked ${count} times`;
}, [count]); // Only re-run the effect if count changes

In the example above, we pass [count] as the second argument.在上面的示例中,我们将 [count] 作为第二个参数传递。 What does this mean?这是什么意思? If the count is 5, and then our component re-renders with count still equal to 5, React will compare [5] from the previous render and [5] from the next render.如果 count 是 5,然后我们的组件重新渲染 count 仍然等于 5,React 将比较前一次渲染的 [5] 和下一次渲染的 [5]。 Because all items in the array are the same (5 === 5), React would skip the effect.因为数组中的所有项都是相同的(5 === 5),所以 React 会跳过这个效果。 That's our optimization.这就是我们的优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM