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

Function Example

examples/preact/suspense/src/index.tsx:35–85  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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