In my functional component i have state and whenever it changes i want to do some stuff. So i used useEffect, but it gives "dependency missing" error. After i put all the dependencies shown in the error i get infinite loop. Here is the code:
const [keywords, setKeywords] = useState<Keyword[]>();
const [currentPage, pagingConfig, setPagingConfig] = usePaging();
useEffect(() => {
const loadKeywords = async () => {
const { current, pageSize } = pagingConfig;
const keywords = await getKeywords(current as number, pageSize as number);
setKeywords(keywords.data.data);
setPagingConfig({
...pagingConfig,
total: keywords.data.total,
});
};
loadKeywords();
}, [currentPage]); // Requires to add pagingConfig and setPagingConfig
If required dependencies are added i get infinite loop.
How can i avoid this error?