infinite render with server fetch hook I wrote

One picture is the hook I wrote, and the other picture is how I'm using it; I'm getting a really nasty infinite render, and I'm sure it has to do with dependencies causing useEffect to keep firing, but I can't quite figure out why.
13 Replies
Perfect
Perfect2y ago
need an expert to answer this ^
jack
jack2y ago
update: no matter what i pass as options, its triggering the infinite renders
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
jack
jack2y ago
Me is a next page, Suspend is jsut just a loading wrapper i wrote
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
jack
jack2y ago
ohhh i was trying to memoize the options inside my hook this wouldnt work cuz its getting passed in newly each time ? also is there a more elegant way of doing this do you think, instead of passing an options object to the hook ?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
alan
alan2y ago
there's a version of this https://usehooks-ts.com/react-hook/use-fetch . there, options isn't included in the dep array for the useEffect
usehooks-ts
useFetch() react hook - usehooks-ts
Here is a React Hook which aims to retrieve data on an API using the native Fetch API. I used a reducer to separate state logic and simplify testing via…
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Valhalaar
Valhalaar2y ago
Alternatively, you can add on to your useHttps hook to cache requests that have already come in, given a combination of some values like request URL + previously encountered options. This lets you more ergonomically call useHttps without having to always memoize stuff at the call site, but it will make the hook implementation more complex
jack
jack2y ago
hmm ok thank you all
Valhalaar
Valhalaar2y ago
this is gonna be a hell of a deep dive but it would be instructive to look at the source code for libraries like react query and apollo and see how they do this I've honestly been meaning to do the same thing myself need to do that soon, if only to satisfy my own curiosity
Develliot
Develliot2y ago
What happens if you take fetchData out of the dep array for the use effect and just have url and option in there.
Want results from more Discord servers?
Add your server