DEV Community

Discussion on: Fetching data with React Hooks

Collapse
 
dnafication profile image
Dina

Hi @savagepixie , thanks for the very nice post.

A question, is there still a chance of infinite loop if the recursive function fetchBook returns the same book every time? May be we need another stronger base case. Or it may be handled in the back end by ensuring that the book is never repeated.

Collapse
 
savagepixie profile image
SavagePixie • Edited

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.