Basic Query in React

From the TanStack Query cheat sheet · Queries · verified Jul 2026

Basic Query

Using useQuery for data fetching

typescript
import { useQuery } from '@tanstack/react-query'

// Basic query
function Profile() {
  const { data, error, isLoading } = useQuery({
    queryKey: ['profile'],
    queryFn: fetchProfile
  })

  if (isLoading) return 'Loading...'
  if (error) return 'An error occurred: ' + error.message

  return <div>{data.name}</div>
}

// Query with parameters
function User({ userId }) {
  const { data, error, isLoading } = useQuery({
    queryKey: ['user', userId],
    queryFn: () => fetchUser(userId),
    enabled: !!userId // Only run if userId exists
  })

  return <div>{data?.name}</div>
}
💡 Query keys must be unique and serializable arrays
⚡ Use enabled option to conditionally fetch data
📌 isLoading vs isFetching: loading is initial, fetching includes refetch
🟢 Always handle loading and error states
queriesfetching

More React tasks

Back to the full TanStack Query cheat sheet