【发布时间】:2020-11-10 07:46:10
【问题描述】:
为什么 React 强迫我使用他们的 linter 插件添加我不想要的依赖项?
例如,我希望我的效果仅在某个特定值更改时触发,但 linter 告诉我将偶数函数添加到依赖项中,而我不希望这样。
为什么它强迫我这样做?我能从中得到什么?
/**
* Gets all items, pages, until 250th.
*/
useEffect(() => {
let mounted = true;
if (loadUntil250th && !paginationProps.complete) {
mounted && setLoading(true);
let limit = 250 - paginationProps.page * BATCH_LIMIT;
fetchListItems(paginationProps, limit, paginationProps.page * BATCH_LIMIT)
.then((results) => {
if (mounted) {
setPaginationProps({
...paginationProps,
page: 250 / BATCH_LIMIT,
autoLoad: false,
complete: paginationProps.totalItems <= 250,
});
setListItems(results.listItems);
setLoading(false);
}
})
.catch((err) => {
logger.log('LOADMORE FAILED:', err);
mounted && setPaginationProps({ ...paginationProps, complete: true });
mounted && setLoading(false);
});
}
return () => {
mounted = false;
};
}, [loadUntil250th]);
它想要这个依赖数组,导致无限循环
[loadUntil250th, logger, paginationProps, setListItems]);
如果我不想要它们,我想了解为什么需要它。
【问题讨论】:
标签: reactjs use-effect