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

index.jsx

73 lines2.0 KB

1import * as React from "react";
2import * as ReactDOM from "react-dom/client";
3import { createBrowserRouter, RouterProvider } from "react-router-dom";
4import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
5import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
6​
7import "./index.css";
8​
9import ErrorPage from "./error-page";
10import Root, {
11 loader as rootLoader,
12 action as rootAction,
13} from "./routes/root";
14import Contact, {
15 loader as contactLoader,
16 action as contactAction,
17} from "./routes/contact";
18import EditContact, { action as editAction } from "./routes/edit";
19import { action as destroyAction } from "./routes/destroy";
20import Index from "./routes/index";
21​
22const queryClient = new QueryClient({
23 defaultOptions: {
24 queries: {
25 staleTime: 1000 * 10,
26 },
27 },
28});
29​
30const router = createBrowserRouter([
31 {
32 path: "/",
33 element: <Root />,
34 errorElement: <ErrorPage />,
35 loader: rootLoader(queryClient),
36 action: rootAction(queryClient),
37 children: [
38 {
39 index: true,
40 element: <Index />
41 },
42 {
43 path: "contacts/:contactId",
44 element: <Contact />,
45 loader: contactLoader(queryClient),
46 action: contactAction(queryClient),
47 },
48 {
49 path: "contacts/:contactId/edit",
50 element: <EditContact />,
51 loader: contactLoader(queryClient),
52 action: editAction(queryClient),
53 },
54 {
55 path: "contacts/:contactId/destroy",
56 element: <EditContact />,
57 action: destroyAction(queryClient),
58 errorElement: <div>Oops! There was an error.</div>
59 },
60 ],
61 },
62 ],
63);
64​
65const rootElement = document.getElementById("root");
66ReactDOM.createRoot(rootElement).render(
67 <React.StrictMode>
68 <QueryClientProvider client={queryClient}>
69 <RouterProvider router={router} />
70 <ReactQueryDevtools position="bottom-right" />
71 </QueryClientProvider>
72 </React.StrictMode>
73);