我在我的应用程序中使用反应表与服务器端分页和搜索。每当分页发生变化时,我都会使用onPaginationChangeprop 来调用 API。但我还有一个搜索输入文本。为此,我使用useEffect监听搜索文本更改并调用 API。
<Pagination
onPaginationChange={(pageSize, pageNo) => {
setNoOfRecords(pageSize);
dispatchGet(
dispatch,
currentOrg.id,
pageSize,
pageNo,
searchText,
);
}} //this is ok
/>
Run Code Online (Sandbox Code Playgroud)
我还有一个searchText状态,useEffect用于searchText更改和 API 调用:
const [searchText, setSearchText] = useState("");
Run Code Online (Sandbox Code Playgroud)
useEffect(() => {
if (currentOrg) {
dispatchGetSubOrgs(
dispatch,
currentOrg.id,
noOfRecords,
currentPage, // I get these from redux store and get updated when API calls
searchText,
);
}
}, [searchText]);
Run Code Online (Sandbox Code Playgroud)
这里 Eslint 抱怨我需要添加currentPage到依赖项数组中。但是,如果我添加它并onPaginationChange由于某些分页更改而被调用,currentPage则会更新并被useEffect调用并将调用 …