#How is the best way to handle this with deconstructing and Select?

3 messages · Page 1 of 1 (latest)

austere flare
#
  return useQuery({
    queryKey: ['auth'],
    queryFn: async () => {
      const response = await fetch(`url`);
      return response.json();
    },
    refetchInterval: 300000,
    select: (data) => {
      return {
        user: data,
      };
    },
  });
}```

and within the component I want to do 

const { data } = useAuth()
const { user } = data


but I get the error that user doesnt exist because data is undefined
balmy jewel
#

Hi. Yes this is the normal behaviour : while loading for ex, the data of the query are undefined. You could either use placeholderdata/initialdata in the query. Or set a default value while destructuring.