简体   繁体   English

避免向 useEffect 添加不必要的依赖项

[英]Avoid adding unnecessary dependencies to useEffect

I am using react table in my app with server side pagination with search.我在我的应用程序中使用带有搜索的服务器端分页的反应表。 Whenever pagination changes I use onPaginationChange prop to call API.每当分页发生变化时,我都会使用onPaginationChange调用 API。 But I also have a search input text.但我也有一个搜索输入文本。 For this I use useEffect to listen to search text changes and call API.为此,我使用useEffect来监听搜索文本的变化并调用 API。

<Pagination 
    onPaginationChange={(pageSize, pageNo) => {
        setNoOfRecords(pageSize);
        dispatchGet(
            dispatch,
            currentOrg.id,
            pageSize,
            pageNo,
            searchText,
        );
    }} //this is ok
/>

I also have a searchText state and useEffect for searchText change and API call:我还有一个searchText状态和useEffect用于searchText更改和 API 调用:

const [searchText, setSearchText] = useState("");
useEffect(() => {
    if (currentOrg) {
        dispatchGetSubOrgs(
            dispatch,
            currentOrg.id,
            noOfRecords,
            currentPage, // I get these from redux store and get updated when API calls
            searchText,
        );
    }
}, [searchText]);

Here Eslint complains that I need to add currentPage to dependency array.这里 Eslint 抱怨我需要将currentPage添加到依赖数组。 But if I add it and onPaginationChange gets called due to some pagination changes, currentPage will be updated and useEffect gets called and will call the API twice.但是,如果我添加它并且由于某些分页更改而调用onPaginationChange ,则currentPage将被更新并调用useEffect并将调用 API 两次。

If I ignore this Eslint error, will it be a problem?如果我忽略这个 Eslint 错误,会不会有问题? Also, I don't know why React wants me to add everything in dependency array.另外,我不知道为什么 React 要我在依赖数组中添加所有内容。 What if I don't want the useEffect to run when something in the dependency array changes?如果我不想在依赖数组中的某些内容发生更改时运行useEffect怎么办? I'm forced to add it because it might have stale values.我被迫添加它,因为它可能具有陈旧的值。 How do I deal with this?我该如何处理?

For useEffect , here is the mindset: "Everything that's defined outside of me, and that my callback function uses, needs to be in my dependencies' array, so I know when I ask the callback to execute again".对于useEffect ,这是一种心态:“在我之外定义的所有东西,以及我的回调函数使用的东西,都需要在我的依赖项的数组中,所以我知道什么时候我要求回调再次执行”。

That's how Eslint sees things.这就是埃斯林特的看法。 But you as developer can make your own choices about what should be in that array.但是作为开发人员,您可以自行选择该数组中的内容。 You can turn off those warnings with the help of eslint-disable-next-line react-hooks/exhaustive-deps , like so:您可以在eslint-disable-next-line react-hooks/exhaustive-deps的帮助下关闭这些警告,如下所示:

 useEffect(() => {
    if (currentOrg) {
      dispatchGetSubOrgs(
        dispatch,
        currentOrg.id,
        noOfRecords,
        currentPage,
        searchText,
      );
    }
  // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [searchText]

But you have to know that, doing so systematically might create bugs in the futur, as you loose those warnings.但是你必须知道,系统地这样做可能会在未来产生错误,因为你会失去这些警告。 However useEffect will work fine, as you decided it should.但是useEffect可以正常工作,正如您决定的那样。

you can add the following comments at the end of the code您可以在代码末尾添加以下注释

       if (currentOrg) {
         dispatchGetSubOrgs(
           dispatch,
           currentOrg.id,
           noOfRecords,
           currentPage, 
           searchText,
         );
       }
   // eslint-disable-next-line react-hooks/exhaustive-deps    
     }, [searchText]

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

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