Theo's Typesafe CultTTC
Theo's Typesafe Cult4y ago
178 replies
stanisław

Prevent possibly undefined user with useQuery

I have a hook useUser that looks basically like this
return useQuery(// fetch user)

Lets imagine that its used in a Layout so when the user is loading the whole app is just a loading spinner.
Now I want to get the user inside a component nested in Layout
Using useUser hook makes no sense here because its return is always User | undefined as typescript doesn't know the user is loaded somewhere higher. How do I solve this problem? Checking
if(!user) return 

Doesn't seem good because the user is always here if the component rendered.
Was this page helpful?