File: suspense.md | Updated: 11/15/2025
Search...
+ K
Auto
Docs Examples GitHub Contributors
Docs Examples GitHub Contributors
Docs Examples GitHub Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Maintainers Partners Support Learn StatsBETA Discord Merch Blog GitHub Ethos Brand Guide
Documentation
Framework
React
Version
v4
Search...
+ K
Menu
Getting Started
Guides & Concepts
Community Resources
API Reference
ESLint
Plugins
Examples
Framework
React
Version
v4
Menu
Getting Started
Guides & Concepts
Community Resources
API Reference
ESLint
Plugins
Examples
React Example: Suspense
===================================================================================================================================================================================================================================================================================================================================================================================================
Code ExplorerCode
Interactive SandboxSandbox
public
src
components
index.jsx
queries.js
.eslintrc
.gitignore
README.md
index.html
package.json
jsx
import React, { lazy } from 'react'
import ReactDOM from 'react-dom/client'
import {
useQueryClient,
QueryClient,
QueryClientProvider,
QueryErrorResetBoundary,
} from '@tanstack/react-query'
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
import { ErrorBoundary } from 'react-error-boundary'
import { fetchProjects } from './queries'
import Button from './components/Button'
const Projects = lazy(() => import('./components/Projects'))
const Project = lazy(() => import('./components/Project'))
const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: 0,
suspense: true,
},
},
})
function App() {
return (
<QueryClientProvider client={queryClient}>
<Example />
</QueryClientProvider>
)
}
function Example() {
const queryClient = useQueryClient()
const [showProjects, setShowProjects] = React.useState(false)
const [activeProject, setActiveProject] = React.useState(null)
return (
<>
<Button
onClick={() => {
setShowProjects((old) => {
if (!old) {
queryClient.prefetchQuery({
queryKey: ['projects'],
queryFn: fetchProjects,
})
}
return !old
})
}}
>
{showProjects ? 'Hide Projects' : 'Show Projects'}
</Button>
<hr />
<QueryErrorResetBoundary>
{({ reset }) => (
<ErrorBoundary
fallbackRender={({ error, resetErrorBoundary }) => (
<div>
There was an error!{' '}
<Button onClick={() => resetErrorBoundary()}>Try again</Button>
<pre style={{ whiteSpace: 'normal' }}>{error.message}</pre>
</div>
)}
onReset={reset}
>
<React.Suspense fallback={<h1>Loading projects...</h1>}>
{showProjects ? (
activeProject ? (
<Project
activeProject={activeProject}
setActiveProject={setActiveProject}
/>
) : (
<Projects setActiveProject={setActiveProject} />
)
) : null}
</React.Suspense>
</ErrorBoundary>
)}
</QueryErrorResetBoundary>
<ReactQueryDevtools initialIsOpen />
</>
)
}
const rootElement = document.getElementById('root')
ReactDOM.createRoot(rootElement).render(<App />)
import React, { lazy } from 'react'
import ReactDOM from 'react-dom/client'
import {
useQueryClient,
QueryClient,
QueryClientProvider,
QueryErrorResetBoundary,
} from '@tanstack/react-query'
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
import { ErrorBoundary } from 'react-error-boundary'
import { fetchProjects } from './queries'
import Button from './components/Button'
const Projects = lazy(() => import('./components/Projects'))
const Project = lazy(() => import('./components/Project'))
const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: 0,
suspense: true,
},
},
})
function App() {
return (
<QueryClientProvider client={queryClient}>
<Example />
</QueryClientProvider>
)
}
function Example() {
const queryClient = useQueryClient()
const [showProjects, setShowProjects] = React.useState(false)
const [activeProject, setActiveProject] = React.useState(null)
return (
<>
<Button
onClick={() => {
setShowProjects((old) => {
if (!old) {
queryClient.prefetchQuery({
queryKey: ['projects'],
queryFn: fetchProjects,
})
}
return !old
})
}}
>
{showProjects ? 'Hide Projects' : 'Show Projects'}
</Button>
<hr />
<QueryErrorResetBoundary>
{({ reset }) => (
<ErrorBoundary
fallbackRender={({ error, resetErrorBoundary }) => (
<div>
There was an error!{' '}
<Button onClick={() => resetErrorBoundary()}>Try again</Button>
<pre style={{ whiteSpace: 'normal' }}>{error.message}</pre>
</div>
)}
onReset={reset}
>
<React.Suspense fallback={<h1>Loading projects...</h1>}>
{showProjects ? (
activeProject ? (
<Project
activeProject={activeProject}
setActiveProject={setActiveProject}
/>
) : (
<Projects setActiveProject={setActiveProject} />
)
) : null}
</React.Suspense>
</ErrorBoundary>
)}
</QueryErrorResetBoundary>
<ReactQueryDevtools initialIsOpen />
</>
)
}
const rootElement = document.getElementById('root')
ReactDOM.createRoot(rootElement).render(<App />)
[###### Want to Skip the Docs?
Query.gg - The Official React Query Course
\
“If you’re serious about *really* understanding React Query, there’s no better way than with query.gg”—Tanner Linsley
Learn More](https://query.gg/?s=tanstack)
You are currently reading v4 docs. Redirect to latest version?
Latest Hide
