https://supertokens.com/ logo
Title
d

d3adb0y

01/24/2023, 3:09 PM
howdy there folks. have a question for you regarding the useSessionContext in React for SuperTokens. I am using TanStack Query to handle my various data fetching needs in my application. I have an API endpoint called user_info which is reachable at the endpoint
/me/
which provides various information about the current users session. And instead of passing the userId down the component tree, it would be much easier if I could just read the userId inside my useQuery call.
export const useGetUserInfo = ({config}: UseUserInfoOptions) => {
  let userId = getUserId()
  
  return useQuery<ExtractFnReturnType<QueryFnType>>({
    ...config,
    queryKey: ['userInfo', userId],
    queryFn: () => getUserInfo(),
    enabled: !!userId
  });
};
I am pretty new to working with React so what I'm trying to figure out is, do I need to make this function and the components that depends on it async in order for this to work properly?
r

rp

01/24/2023, 4:25 PM
hey!
You don't need to make things async. Just use our useSessionContext hook in your component and read the user ID from the resulting session object.
d

d3adb0y

01/24/2023, 5:00 PM
thanks for the quick response as always @rp
yeah i think im just trying to over optimize a bit
now that ive drank the JS koolaid lol
i moved back to just passing in the user ID
r

rp

01/24/2023, 5:01 PM
Why do you need to pass the user id to the backend? Just use the session tokens
d

d3adb0y

01/24/2023, 5:02 PM
yeah i just realized that there really isnt a need to use userId as a query key at all
smh
r

rp

01/24/2023, 5:02 PM
Yup
d

d3adb0y

01/24/2023, 5:03 PM
well that cleans up my code nicely. ive been up for 24 hours on a marathon coding session so clearly my brain no worky so good 😄
after hitting my head on the desk daily the skies are finally parting and my app is coming together.