Skip to content
Advertisement

I can fetch data based on my query, but then the fetching never stops. How do I limit the fetch requests?

As I type in my search, the data will fetch from the API properly. The problem is that it keeps fetching several thousand times until the API throws an error. I successfully implemented an abortController, but I feel like there’s a less abrupt way to stop a fetch. I am using the useEffect hook based on the query and inventory state, but I don’t think my logic is very concise at all. Ideally, I need to fetch the data once and wait to fetch it again when the query state changes. Not necessarily the query and inventory as it is in the code, but that’s the only way it seemed to work.

Note: If there is nothing in the query, no results should be displayed.

Here’s the API (not sure if it’s needed for this question):

JavaScript

Here’s the Search component:

JavaScript

Advertisement

Answer

Because you have put bookInventory in Second argument to useEffect, and put setBookInventory in useEffect function. This means every time bookInventory changes, call useEffect.
This’s the reason of that it keeps fetching.

The solution is to delete bookInventory from Second argument to useEffect:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement