简体   繁体   English

useEffect 与 recyclerlistview (React Native) 和穷举-deps

[英]useEffect with recyclerlistview (React Native) and exhaustive-deps

I am using a useEffect to fetch data on component render and set it to React Native RecyclerView as a data provider.我正在使用 useEffect 来获取组件渲染上的数据并将其设置为 React Native RecyclerView 作为数据提供者。 I then set the state locally by appending to the existing dataProviderState.然后我通过附加到现有的 dataProviderState 在本地设置 state。 The problem is when I add the state as a dependency to the array the performance is terrible, as now the useEffect is called every time the list changes, so whenever the list is scrolled.问题是当我将 state 添加为数组的依赖项时,性能很糟糕,因为现在每次列表更改时都会调用 useEffect,因此每当滚动列表时。 How can I rewrite this hook to make this more performant while also following the exhaustive deps rule?如何在遵循详尽的 deps 规则的同时重写这个钩子以提高性能?

  const dataProvider = new DataProvider((r1, r2) => r1 !== r2);

  const rows = dataProvider.cloneWithRows([]);

  const [dataProviderState, setDataProviderState] = useState(rows);

  useEffect(() => {
    const handleFetchContacts = async () => {
      try {
        const fetchedContacts = await fetchContacts();
        setContacts(fetchedContacts);
        setDataProviderState(dataProviderState.cloneWithRows(fetchedContacts));
      } catch (error) {
        logger.error(error);
      }
    };

    const requestPermissionAndFetchContacts = async () => {
      try {
        const permission = await requestPermissionToContacts();
        setPermissions({contacts: permission});

        if (permission) {
          handleFetchContacts();
        }
      } catch (err) {
        logger.error(err);
        setTimeout(() => requestPermissionAndFetchContacts(), 2000);
      }
    };
    requestPermissionAndFetchContacts();
  }, [handleFetchContacts]);

^adding dataProviderState to the dependency array here makes the list slow ^在此处将 dataProviderState 添加到依赖数组会使列表变慢

I was able to fix this by referencing the previous state in the callback, so exhaustive-deps stopped complaining about me referencing the state value directly inside the component, but not having it in the array:我可以通过在回调中引用之前的 state 来解决此问题,因此详尽的部门不再抱怨我直接在组件内部引用 state 值,但没有在数组中包含它:

setDataProviderState(state => state.cloneWithRows(fetchedContacts));

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

相关问题 反应 useEffect 带有警告的钩子 react-hooks/exhaustive-deps - React useEffect hook with warning react-hooks/exhaustive-deps useEffect 和 'react-hooks/exhaustive-deps' linting - useEffect and 'react-hooks/exhaustive-deps' linting 了解 react-hooks/exhaustive-deps useEffect 和调度事件 - understanding react-hooks/exhaustive-deps useEffect and dispatching events 使用 eslint Exclusive-deps 响应订阅/取消订阅的 useEffect 依赖项 - React useEffect dependencies for subscribe/unsubscribe with eslint exhaustive-deps useEffect 详尽-deps 规则令人困惑 - useEffect exhaustive-deps rule is confusing useEffect 和 ESlint 穷举-deps 规则 - useEffect and ESlint exhaustive-deps rule React useEffect 在发布时给出 react-hooks/exhaustive-deps 错误 - React useEffect gives react-hooks/exhaustive-deps error on publishing React linter 建议在 useEffect react-hooks/exhaustive-deps 警告中用 useMemo 包装常量数组 - React linter suggesting to wrap constant array with useMemo inside useEffect react-hooks/exhaustive-deps warning 如何解决`react-hooks/exhaustive-deps`的`React Hook useEffect缺少依赖`? - How to solve `React Hook useEffect has a missing dependency` of `react-hooks/exhaustive-deps`? 在响应中添加 useEffect 的正确方法来调用一次以满足 eslint react-hooks/exhaustive-deps? - Proper way to add useEffect in react to call once to satisfy eslint react-hooks/exhaustive-deps?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM