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

Function Example

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 2

useQueryClientFunction · 0.90
prefetchQueryMethod · 0.45

Tested by

no test coverage detected