DEV Community

Discussion on: Fetching data with React Hooks

savagepixie profile image
SavagePixie Author • Edited on

Thanks for your reply!

A question, is there still a chance of infinite loop if the recursive function fetchBook returns the same book every time?

Very good question. To tell the truth, I'm not completely sure. I think it wouldn't be an infinite loop, but it'd run twice (once after the component is mounted and once after the state is updated), which is still undesirable and should be addressed. I'll double-check when I get the chance, though.

EDIT: Okay, after a quick experiment, it seems to run into an infinite loop even if the data fetched is always the same.

Or it may be handled in the back end by ensuring that the book is never repeated.

We certainly can handle it in the backend. The principle would be the same, keep track of the last book sent and ensure it doesn't get repeated.