trpc or react-query like set-up for just 1 time calls
Right now I have a handler under /api/something and then I fetch(' /api/something') inside useEffect(). Is there a way to use trpc/react query to just do an api call *once like useEffect(() => {}, []) empty dependency array??
20 Replies
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
yeah that's what I arrived at too, I was thinking there would be some other elegant way so I don't have to setState of whatever comes out of fetch you know
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
If you're using tRPC aren't there vanilla methods that aren't hooks? You might be able to use them inside useEffect.
Are you able to reference this? I couldn't find it
Vanilla client | tRPC
The magic of tRPC is making strongly typed API calls without relying on code generation. With full-stack TypeScript projects, you can directly import types from the server into the client! This is a vital part of how tRPC works.
You can do operations with vanillla
query
and mutation
methods, which should be fine inside a useEffectHm, I might have tried this. Let me check again and see if it works.
This looks like just normal rq
useQuery
usage? Yeah as long as you disable refetchOnWindowFocus
it should just run once when the component is loadedUnknown User•2y ago
Message Not Public
Sign In & Join Server To View
Ok, so that's all I need to disable?
Here is reference of what I am trying to do:
https://stripe.com/docs/payments/quickstart
It is very critical that it only runs once and I was iffy about all the things I need to disable
if you're posting (which is usually a mutation) you would do something like
If it's a query, that's a little tricker, but something like
So in this particular case:
I tried mutation before, but it was difficult to use since you need to do .mutate() somehwere, and doing it in useEffect will cause infinite loop. Then useQuery can also be used but I am not certain if all refetch options are disabled
yeah, just disable refetch on window focus, that's the only thing that causes them to be ran multiple times
I disable that globally, v annoying
react-query basically does what you just put but better and with a cache
What about this option:
I usually just leave that alone? unmounting is the component leaving the page
so like, if you leave the page and come back you want it to rerun
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View