Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

index.tsx

88 lines2.4 KB

1import {
2 QueryClient,
3 QueryClientProvider,
4 QueryErrorResetBoundary,
5 noop,
6 useQueryClient,
7} from '@tanstack/react-query'
8import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
9import React, { lazy } from 'react'
10import ReactDOM from 'react-dom/client'
11import { ErrorBoundary } from 'react-error-boundary'
12import Button from './components/Button'
13import { projectsQueries } from './services/projects/queries'
14​
15const Projects = lazy(() => import('./components/Projects'))
16const Project = lazy(() => import('./components/Project'))
17​
18const queryClient = new QueryClient({
19 defaultOptions: {
20 queries: {
21 retry: 0,
22 },
23 },
24})
25​
26function App() {
27 return (
28 <QueryClientProvider client={queryClient}>
29 <Example />
30 </QueryClientProvider>
31 )
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 )
85​
86const rootElement = document.getElementById('root')
87if (!rootElement) throw new Error('Missing #root element')
88ReactDOM.createRoot(rootElement).render(<App />)