I am trying to get isLoading returned from a custom React Query hook.
Here is my hook:
async function getUserDiscussions(
user: User | null
): Promise<Post[] | null> {
const { data } = await axiosInstance.get(
`/getUserPosts`,
);
return data;
}
export function usePosts(): Post[] {
const fallback: Post[] = [];
const { data: posts = fallback } = useQuery(
[
queryKeys.posts,
],
() => getUserDiscussions(user),
);
return posts;
}
I am then calling the hook in a component like this:
const posts = usePosts()
I would like to then be able to access isLoading and update the UI accordingly.
How can I access isLoading from the query in the custom hook and use it in the component?
isLoadingmethod. Also you can return the whole query obj.return useQuery(...)instead ofpostsandisLoading.