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

index.jsx

91 lines2.4 KB

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 suspense: true,
24 },
25 },
26});
27​
28function App() {
29 return (
30 <QueryClientProvider client={queryClient}>
31 <Example />
32 </QueryClientProvider>
33 );
35​
36function Example() {
37 const queryClient = useQueryClient();
38 const [showProjects, setShowProjects] = React.useState(false);
39 const [activeProject, setActiveProject] = React.useState(null);
40​
41 return (
42 <>
43 <Button
44 onClick={() => {
45 setShowProjects((old) => {
46 if (!old) {
47 queryClient.prefetchQuery(["projects"], fetchProjects);
48 }
49 return !old;
50 });
51 }}
52 >
53 {showProjects ? "Hide Projects" : "Show Projects"}
54 </Button>
55​
56 <hr />
57​
58 <QueryErrorResetBoundary>
59 {({ reset }) => (
60 <ErrorBoundary
61 fallbackRender={({ error, resetErrorBoundary }) => (
62 <div>
63 There was an error!{" "}
64 <Button onClick={() => resetErrorBoundary()}>Try again</Button>
65 <pre style={{ whiteSpace: "normal" }}>{error.message}</pre>
66 </div>
67 )}
68 onReset={reset}
69 >
70 <React.Suspense fallback={<h1>Loading projects...</h1>}>
71 {showProjects ? (
72 activeProject ? (
73 <Project
74 activeProject={activeProject}
75 setActiveProject={setActiveProject}
76 />
77 ) : (
78 <Projects setActiveProject={setActiveProject} />
79 )
80 ) : null}
81 </React.Suspense>
82 </ErrorBoundary>
83 )}
84 </QueryErrorResetBoundary>
85 <ReactQueryDevtools initialIsOpen />
86 </>
87 );
89​
90const rootElement = document.getElementById("root");
91ReactDOM.createRoot(rootElement).render(<App />);