I have a useEffect function which runs only when the page state changes, but I want to run this function on both component mount as well as state change.
To accomplish this I used two variables in dependency array
, [page, []]
Using this expression in dependency array shows a warning in the console and the useEffect method runs infinitely, I don't know how to fix this.
My useEffect funtion is
// fetch data from the server when page changes
useEffect(() => {
axios
.get(fetchURL, { headers: { Authorization: AuthStr } })
.then((response) => {
// If request is good...
if (response) {
setData(response.data);
}
})
.catch((error) => {
toast.error('Something went wrong, please refresh');
console.log(error);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [page]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
Please suggest a possible solution to this problem.