So I'm trying to implement React Query to the point where the result is set into my Jotai atoms. I'm running into this error when I try to use it:
WARN Query data cannot be undefined.Please make sure to return a value other than undefined from your query function.Affected query key: ["user-profile", "THIS IS A USER ID"]WARN [Error: undefined]
This is what I have going on:
userProfileAtom.ts
This is a logged-in user profile atom that I use across my app.
import { atom } from 'jotai';import { SupabaseUserProfile } from '@interfaces/interfaces';export const userProfileAtom = atom<SupabaseUserProfile | null>(null);
getProfileById.ts
This is the structure of the query to get a single profile back from the database.
export function getProfileById(client: SupabaseClient, profileId: string) { return client .from('profiles') .select('*') .eq('id', profileId) .throwOnError() .single();}
useProfileQuery.ts
This is the hook I created that I use in my components to make the request. Here I am trying to set my atom to the result.data of the request. This is also where the error occurs.
function useProfileQuery(profileId: string) { const client = useSupabase(); const key = ['user-profile', profileId]; const [, setUserProfile] = useAtom(userProfileAtom); return useQuery(key, async () => { return getProfileById(client, profileId).then(result => setUserProfile(result.data), ); });}export default useProfileQuery;
What am I doing wrong in handing in the result.data?
My component that uses it example:
const { data, isLoading } = useProfileQuery(userID);