The useQuery hook accepts a configuration object with key properties:
const { data, isLoading, error, isError, isSuccess } = useQuery({
queryKey: ['posts', userId], // Unique identifier for the query
queryFn: () => fetchPosts(userId), // Function that returns a promise
enabled: !!userId, // Optional: conditionally enable the query
staleTime: 5 * 60 * 1000, // Optional: how long data stays fresh
refetchOnWindowFocus: false // Optional: disable refetch on window focus
});
Key properties:
queryKey: Unique identifier used for caching and invalidationqueryFn: Async function that fetches the dataenabled: Boolean to conditionally run the querystaleTime: Time in milliseconds before data is considered staledata: The fetched dataisLoading: True during the first fetcherror: Error object if the query failedisError/isSuccess: Boolean states for handling different scenarios