tRPC: Return undefined if param is not there
In this code cityById could be undefined and I want that this checks this and then weatherData gets undefined.
Problem: I can't wrap this in a condition because of React's Hook Rules
16 Replies
you can use a
enabled: false
to block the hook to run before something happensthank you! do you mean something like this?
if not do you have an example for me?
something like that yeah
Seems not to work π«€
the execution still runs
only the http call itself isnt dispatched
you should still use
cityById?.coord
but then I have to deal with a possible undefined value in my backend
But enabled holds back the undefined values
the problem is not that it works the problem is that typescript yells at me
Then the problem isnβt trpc
Just know ts better or make ts ignore the line
@nyx (Rustular DevRel) the absolute legendary KATT sent me this part of the docs so I will try this out: https://trpc.io/docs/client/react/disabling-queries#typesafe-conditional-queries-using-skiptoken
The problem is just that I'm on version 10 of tRPC and this a v11 feature
Disabling Queries | tRPC
To disable queries, you can pass skipToken as the first argument to useQuery or useInfiniteQuery. This will prevent the query from being executed.
How about
If that doesn't work, try passing
cityById?.coord ?? -1
to see what actually gets passed.I think I just update to v11 since I think I will have to update anyways in the next few months.
v11 doesnβt have many breaking changes but has React Query v5 as a peer dependency, which does. Hopefully thatβs not a problem, assuming you donβt have too many of
.useQuery
I ran into this error and still figuring this out. I hope I find the solution soon: https://discord.com/channels/966627436387266600/1218145640752877708
Hopefully, but itβd probably be much easier and faster to debug this
enabled
thing. Are you on the latest v10? I am, and it works perfectly fine for me.OK, yes, maybe I really have to do it