简体   繁体   English

是否有一个带有useEffect的无限滚动的实现,它不会触发react-hooks / exhaustive-deps lint警告?

[英]Is there an implementation of infinite scroll with useEffect that doesn't trigger the react-hooks/exhaustive-deps lint warning?

I'm implementing an infinite scroll-like solution using React hooks. 我正在使用React钩子实现无限滚动式解决方案。 The following code shows what I'm trying to accomplish and a working example can be seen on codesandbox here . 下面的代码显示什么,我试图完成和工作的例子可以在codesandbox可以看出这里

I'm receiving an exhaustive-deps warning in useEffect because I haven't listed data as a dependency (this would obviously cause an infinite loop). 我在useEffect收到exhaustive-deps警告,因为我没有将data列为依赖项(这显然会导致无限循环)。 Is there a way to implement this kind of infinite scrolling solution without suppressing this warning? 有没有办法实现这种无限滚动解决方案而不会抑制此警告?

function App() {
  const [data, setData] = useState([]);
  const [page, setPage] = useState(0);

  useEffect(() => {
    const newData = getData(page);
    setData([...data, ...newData]);
  }, [page, setData]); // react-hooks/exhaustive-deps warning here

  return (
    <div>
      Data:
      <ul>
        {data.map(el => (
          <li>{el}</li>
        ))}
      </ul>
      <button onClick={() => setPage(page + 1)}>Load More</button>
    </div>
  );
}

You can pass a callback to receive the previous data. 您可以传递回调以接收以前的数据。

  useEffect(() => {
    const newData = getData(page);
    setData(previousData => [...previousData, ...newData]);
  }, [page, setData]);

Forked sandbox without warnings. 叉沙箱没有警告。
编辑so.answer.57564952


Unrelated to the warning above, also make sure to add key prop for each item. 与上述警告无关,还要确保为每个项目添加key道具。

 <li key={el}>{el}</li>

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

相关问题 反应 useEffect 带有警告的钩子 react-hooks/exhaustive-deps - React useEffect hook with warning react-hooks/exhaustive-deps react-hooks/exhaustive-deps 警告还是无限循环? - react-hooks/exhaustive-deps warning or infinite loop? useEffect 和 &#39;react-hooks/exhaustive-deps&#39; linting - useEffect and 'react-hooks/exhaustive-deps' linting 了解 react-hooks/exhaustive-deps useEffect 和调度事件 - understanding react-hooks/exhaustive-deps useEffect and dispatching events 由 axios 取消令牌引起的 react useEffect hook 无限循环 [react-hooks/exhaustive-deps] - react useEffect hook infinite loop caused by axios cancel token [react-hooks/exhaustive-deps] ESLint 希望 setSate 作为 useEffect 的依赖,但这会导致无限循环(react-hooks/exhaustive-deps) - ESLint wants setSate as a dependency for useEffect but this causes an infinite loop (react-hooks/exhaustive-deps) 从 useEffect 中改变多个状态会导致 react-hooks/exhaustive-deps 警告 - Mutate multiple states from within useEffect causes react-hooks/exhaustive-deps warning 带有自定义 IntersectionObserver 钩子的 react-hooks/exhaustive-deps 警告 - react-hooks/exhaustive-deps warning with custom IntersectionObserver hook useCallBack react-hooks/exhaustive-deps 警告 - useCallBack react-hooks/exhaustive-deps warning 解决 react-hooks/exhaustive-deps 警告的最佳方法 - Best way to resolve react-hooks/exhaustive-deps warning
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM