Open History to see the last commit
1import React, { lazy } from 'react'
2import ReactDOM from 'react-dom/client'
3import {
4 useQueryClient,
5 QueryClient,
6 QueryClientProvider,
7 QueryErrorResetBoundary,
8} from '@tanstack/react-query'
9import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
10import { ErrorBoundary } from 'react-error-boundary'
11β
12import { fetchProjects } from './queries'
13β
14import Button from './components/Button'
15β
16const Projects = lazy(() => import('./components/Projects'))
17const Project = lazy(() => import('./components/Project'))
18β
19const queryClient = new QueryClient({
20 defaultOptions: {
21 queries: {
22 retry: 0,
23 },
24 },
25})
26β
27function App() {
28 return (
29 <QueryClientProvider client={queryClient}>
30 <Example />
31 </QueryClientProvider>
32 )
33}
34β
35function Example() {
36 const queryClient = useQueryClient()
37 const [showProjects, setShowProjects] = React.useState(false)
38 const [activeProject, setActiveProject] = React.useState(null)
39β
40 return (
41 <>
42 <Button
43 onClick={() => {
44 setShowProjects((old) => {
45 if (!old) {
46 queryClient.prefetchQuery({
47 queryKey: ['projects'],
48 queryFn: fetchProjects,
49 })
50 }
51 return !old
52 })
53 }}
54 >
55 {showProjects ? 'Hide Projects' : 'Show Projects'}
56 </Button>
57β
58 <hr />
59β
60 <QueryErrorResetBoundary>
61 {({ reset }) => (
62 <ErrorBoundary
63 fallbackRender={({ error, resetErrorBoundary }) => (
64 <div>
65 There was an error!{' '}
66 <Button onClick={() => resetErrorBoundary()}>Try again</Button>
67 <pre style={{ whiteSpace: 'normal' }}>{error.message}</pre>
68 </div>
69 )}
70 onReset={reset}
71 >
72 <React.Suspense fallback={<h1>Loading projects...</h1>}>
73 {showProjects ? (
74 activeProject ? (
75 <Project
76 activeProject={activeProject}
77 setActiveProject={setActiveProject}
78 />
79 ) : (
80 <Projects setActiveProject={setActiveProject} />
81 )
82 ) : null}
83 </React.Suspense>
84 </ErrorBoundary>
85 )}
86 </QueryErrorResetBoundary>
87 <ReactQueryDevtools initialIsOpen />
88 </>
89 )
90}
91β
92const rootElement = document.getElementById('root')
93ReactDOM.createRoot(rootElement).render(<App />)