ebleach7

Loader React Router

Mar 9th, 2023 (edited)
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { clubService } from "@/api/services";
  2. import { AppLayout } from "@/layouts";
  3. import {
  4.   AddClub,
  5.   Club,
  6.   Clubs,
  7.   Footballers,
  8.   Home,
  9.   Login,
  10.   News,
  11.   PageNotFound,
  12. } from "@/pages";
  13. import { QueryClient } from "@tanstack/react-query";
  14. import { createBrowserRouter } from "react-router-dom";
  15.  
  16. const queryClient = new QueryClient();
  17.  
  18. export const router = createBrowserRouter([
  19.   {
  20.     element: <AppLayout />,
  21.     children: [
  22.       {
  23.         path: "/",
  24.         element: <Home />,
  25.       },
  26.       {
  27.         path: "/clubs",
  28.         element: <Clubs />,
  29.       },
  30.       {
  31.         path: "/clubs/add",
  32.         element: <AddClub />,
  33.       },
  34.       {
  35.         path: "/clubs/:clubId",
  36.         element: <Club />,
  37.         loader: async ({ params }: { params: any }) => {
  38.           const contactDetailQuery = (id: number) => ({
  39.             queryKey: ["clubs", id],
  40.             queryFn: async () => clubService.getClub(id),
  41.           });
  42.  
  43.           const query = contactDetailQuery(params.clubId);
  44.  
  45.           return (
  46.             queryClient.getQueryData(query.queryKey) ??
  47.             (await queryClient.fetchQuery(query))
  48.           );
  49.         },
  50.         errorElement: <PageNotFound />,
  51.       },
  52.       {
  53.         path: "/footballers",
  54.         element: <Footballers />,
  55.       },
  56.       {
  57.         path: "/news",
  58.         element: <News />,
  59.       },
  60.       {
  61.         path: "/404",
  62.         element: <PageNotFound />,
  63.       },
  64.       {
  65.         path: "/*",
  66.         element: <PageNotFound />,
  67.       },
  68.     ],
  69.   },
  70.   {
  71.     path: "/login",
  72.     element: <Login />,
  73.   },
  74. ]);
  75.  
Advertisement
Add Comment
Please, Sign In to add comment