examples/react/suspense
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack React Query Suspense Example#
Loads projects with suspense queries, and resets failed queries from an error boundary.
How it works#
src/services/projects/queries.tsdefinesprojectsQueries.list()andprojectsQueries.detail(id)withqueryOptions.src/services/projects/api.tsfetches TanStack repositories from the GitHub API and adds a 1-second delay.src/components/Projects.tsxandsrc/components/Project.tsxare lazy-loaded and read their data withuseSuspenseQuery, so theReact.Suspensefallback insrc/index.tsxshows until the data and the component code are ready.- The "Show Projects" button in
src/index.tsxstartsqueryClient.query(projectsQueries.list()), and the "Load" button insrc/components/Projects.tsxstartsqueryClient.query(projectsQueries.detail(project.full_name)), before the state change that renders the component, so fetching runs in parallel with loading the component code. src/components/Button.tsxwraps clicks instartTransitionand shows a spinner while the transitionisPending.src/index.tsxwraps the boundary inQueryErrorResetBoundaryand passes itsresetto theErrorBoundaryfromreact-error-boundary, so "Try again" resets the failed queries before rendering again. TheQueryClientsetsretry: 0so errors show right away.
To run this example:
npm installnpm run dev