i had read some questions and article about this issue including this one: but i don't find my answer.
consider this code.
function Component({ page }) {
const [list, setList] = useState([]);
useEffect(() => {
axios.get([url] + page).then(newList => {
setList([...list, ...newList]);
});
}, [page, list]);
return ['some jsx for map though list'];
};
i have a pagination and every time i go to next page i need to concatenate the data with previous one, here is the deal.
the react complains about dependency array about
list
andpage
.i'm agree about
page
because each time it changes i need to fetch more data. but when i addlist
to dependency arrayuseEffect
find changes inlist
and fetch more data which cause infinit loop.
i know about hooks but i wonder what is the best practise to solve this issue.