I use the useEffect hook to fetch data from database by calling another function getData, when I recieve the data I update the state.
const [employees, setEmployees] = useState([]) const [shownEmployees, setShownEmployees] = useState([]) const [nbrPages, setNbrPages] = useState(0) const getData = () => { axios.request(options).then((response) => { setEmployees(response.data) setShownEmployees(response.data.slice(0, nbrItems)) response.data.length%nbrItems > 0 ? setNbrPages((response.data.length/nbrItems)+1) : setNbrPages(response.data.length/nbrItems) console.log(nbrPages) }).catch((error) => console.log(error)) for(let i=0;i<nbrPages;i++){ pages_temp.push(i) } console.log(pages_temp.length) setPages(pages_temp) } useEffect(() => { getData() },[]) The problem nbrPages which is all the time 0, but when I put employees to useEffect as dependency, useEffect is executing many times but this time the nbrPages is bieng calculated. Can anyone explain this behaviour, I've tried every thing. Essentially I want to understand why useEffect is getting executed infinitely when I give it employees as dependency, at what moment employees changes so it triggers the useEffect hook.
nbrPagesstill equals to 0.