|
2 | 2 |
|
3 | 3 | To run this example:
|
4 | 4 |
|
5 |
| -- `npm install` or `yarn` |
6 |
| -- `npm start` or `yarn start` |
| 5 | +- `npm install` or `yarn` or `pnpm install` or `bun install` |
| 6 | +- `npm start` or `yarn start` or `pnpm start` or `bun run dev` |
7 | 7 |
|
8 | 8 | ## Styling
|
9 | 9 |
|
10 | 10 | This project includes [Tailwind CSS](https://tailwindcss.com/), a utility-first CSS framework.
|
11 | 11 |
|
12 | 12 | ## Routing
|
13 | 13 |
|
14 |
| -This project uses [TanStack Router](https://tanstack.com/router). |
| 14 | +This project uses [TanStack Router](https://tanstack.com/router). The initial setup is a code based router. Which means that the routes are defined in code (in the src/main.tsx file). If you like you can also use a file based routing setup by following the [File Based Routing](https://tanstack.com/router/latest/docs/framework/react/guide/file-based-routing) guide. |
| 15 | + |
| 16 | +### Adding A Route |
| 17 | + |
| 18 | +To add a new route to your application just add another `createRoute` call to the `./src/main.tsx` file. The example below adds a new `/about`route to the root route. |
| 19 | + |
| 20 | +```tsx |
| 21 | +const aboutRoute = createRoute({ |
| 22 | + getParentRoute: () => rootRoute, |
| 23 | + path: "/about", |
| 24 | + component: () => <h1>About</h1>, |
| 25 | +}); |
| 26 | +``` |
| 27 | + |
| 28 | +You will also need to add the route to the `routeTree` in the `./src/main.tsx` file. |
| 29 | + |
| 30 | +```tsx |
| 31 | +const routeTree = rootRoute.addChildren([indexRoute, aboutRoute]); |
| 32 | +``` |
| 33 | + |
| 34 | +With this set up you should be able to navigate to `/about` and see the about page. |
| 35 | + |
| 36 | +Of course you don't need to implement the About page in the `main.tsx` file. You can create that component in another file and import it into the `main.tsx` file, then use it in the `component` property of the `createRoute` call, like so: |
| 37 | + |
| 38 | +```tsx |
| 39 | +import About from "./components/About"; |
| 40 | + |
| 41 | +const aboutRoute = createRoute({ |
| 42 | + getParentRoute: () => rootRoute, |
| 43 | + path: "/about", |
| 44 | + component: About, |
| 45 | +}); |
| 46 | +``` |
| 47 | + |
| 48 | +That is how we have the `App` component set up with the home page. |
| 49 | + |
| 50 | +For more information on the options you have when you are creating code based routes check out the [Code Based Routing](https://tanstack.com/router/latest/docs/framework/react/guide/code-based-routing) documentation. |
| 51 | + |
| 52 | +Now that you have two routes you can use a `Link` component to navigate between them. |
| 53 | + |
| 54 | +### Adding Links |
| 55 | + |
| 56 | +To use SPA (Single Page Application) navigation you will need to import the `Link` component from `@tanstack/react-router`. |
| 57 | + |
| 58 | +```tsx |
| 59 | +import { Link } from "@tanstack/react-router"; |
| 60 | +``` |
| 61 | + |
| 62 | +Then anywhere in your JSX you can use it like so: |
| 63 | + |
| 64 | +```tsx |
| 65 | +<Link to="/about">About</Link> |
| 66 | +``` |
| 67 | + |
| 68 | +This will create a link that will navigate to the `/about` route. |
| 69 | + |
| 70 | +### Using A Layout |
| 71 | + |
| 72 | +Layouts can be used to wrap the contents of the routes in menus, headers, footers, etc. |
| 73 | + |
| 74 | +There is already a layout in the `src/main.tsx` file: |
| 75 | + |
| 76 | +```tsx |
| 77 | +const rootRoute = createRootRoute({ |
| 78 | + component: () => ( |
| 79 | + <> |
| 80 | + <Outlet /> |
| 81 | + <TanStackRouterDevtools /> |
| 82 | + </> |
| 83 | + ), |
| 84 | +}); |
| 85 | +``` |
| 86 | + |
| 87 | +You can use the React component specified in the `component` property of the `rootRoute` to wrap the contents of the routes. The `<Outlet />` component is used to render the current route within the body of the layout. For example you could add a header to the layout like so: |
| 88 | + |
| 89 | +```tsx |
| 90 | +const rootRoute = createRootRoute({ |
| 91 | + component: () => ( |
| 92 | + <> |
| 93 | + <header> |
| 94 | + <nav> |
| 95 | + <Link to="/">Home</Link> |
| 96 | + <Link to="/about">About</Link> |
| 97 | + </nav> |
| 98 | + </header> |
| 99 | + <Outlet /> |
| 100 | + <TanStackRouterDevtools /> |
| 101 | + </> |
| 102 | + ), |
| 103 | +}); |
| 104 | +``` |
| 105 | + |
| 106 | +The `<TanStackRouterDevtools />` component is not required so you can remove it if you don't want it in your layout. |
| 107 | + |
| 108 | +## Data Fetching |
| 109 | + |
| 110 | +There are multiple ways to fetch data in your application. You can use TanStack Query to fetch data from a server. But you can also use the `loader` functionality built into TanStack Router to load the data for a route before it's rendered. |
| 111 | + |
| 112 | +For example: |
| 113 | + |
| 114 | +```tsx |
| 115 | +const peopleRoute = createRoute({ |
| 116 | + getParentRoute: () => rootRoute, |
| 117 | + path: "/people", |
| 118 | + loader: async () => { |
| 119 | + const response = await fetch("https://swapi.dev/api/people"); |
| 120 | + return response.json(); |
| 121 | + }, |
| 122 | + component: () => { |
| 123 | + const data = useLoaderData<typeof loader>(); |
| 124 | + return ( |
| 125 | + <ul> |
| 126 | + {data.results.map((person) => ( |
| 127 | + <li key={person.name}>{person.name}</li> |
| 128 | + ))} |
| 129 | + </ul> |
| 130 | + ); |
| 131 | + }, |
| 132 | +}); |
| 133 | +``` |
0 commit comments