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

Function Example

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 2

useQueryClientFunction · 0.90
queryMethod · 0.45

Tested by

no test coverage detected