MCPcopy Create free account
hub / github.com/TanStack/query / Example

Function Example

examples/react/suspense/src/index.tsx:34–84  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

32}
33
34function Example() {
35 const queryClient = useQueryClient()
36 const [showProjects, setShowProjects] = React.useState(false)
37 const [activeProject, setActiveProject] = React.useState<string | null>(null)
38
39 return (
40 <>
41 <Button
42 onClick={() => {
43 if (!showProjects) {
44 queryClient.query(projectsQueries.list()).catch(noop)
45 }
46 setShowProjects((old) => !old)
47 }}
48 >
49 {showProjects ? 'Hide Projects' : 'Show Projects'}
50 </Button>
51
52 <hr />
53
54 <QueryErrorResetBoundary>
55 {({ reset }) => (
56 <ErrorBoundary
57 fallbackRender={({ error, resetErrorBoundary }) => (
58 <div>
59 There was an error!{' '}
60 <Button onClick={() => resetErrorBoundary()}>Try again</Button>
61 <pre style={{ whiteSpace: 'normal' }}>{error.message}</pre>
62 </div>
63 )}
64 onReset={reset}
65 >
66 {showProjects ? (
67 <React.Suspense fallback={<h1>Loading projects...</h1>}>
68 {activeProject ? (
69 <Project
70 activeProject={activeProject}
71 setActiveProject={setActiveProject}
72 />
73 ) : (
74 <Projects setActiveProject={setActiveProject} />
75 )}
76 </React.Suspense>
77 ) : null}
78 </ErrorBoundary>
79 )}
80 </QueryErrorResetBoundary>
81 <ReactQueryDevtools initialIsOpen />
82 </>
83 )
84}
85
86const rootElement = document.getElementById('root')
87if (!rootElement) throw new Error('Missing #root element')

Callers

nothing calls this directly

Calls 2

useQueryClientFunction · 0.90
queryMethod · 0.45

Tested by

no test coverage detected