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

index.js

93 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 "react-query";
9import { ReactQueryDevtools } from "react-query/devtools";
10import { ErrorBoundary } from "react-error-boundary";
11​
12import "./styles.css";
13​
14import { fetchProjects } from "./queries";
15​
16import Button from "./components/Button";
17​
18const Projects = lazy(() => import("./components/Projects"));
19const Project = lazy(() => import("./components/Project"));
20​
21const queryClient = new QueryClient({
22 defaultOptions: {
23 queries: {
24 retry: 0,
25 suspense: true,
26 },
27 },
28});
29​
30function App() {
31 return (
32 <QueryClientProvider client={queryClient}>
33 <Example />
34 </QueryClientProvider>
35 );
37​
38function Example() {
39 const queryClient = useQueryClient();
40 const [showProjects, setShowProjects] = React.useState(false);
41 const [activeProject, setActiveProject] = React.useState(null);
42​
43 return (
44 <>
45 <Button
46 onClick={() => {
47 setShowProjects((old) => {
48 if (!old) {
49 queryClient.prefetchQuery(["projects"], fetchProjects);
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 );
91​
92const rootElement = document.getElementById("root");
93ReactDOM.createRoot(rootElement).render(<App />);