|
1 |
| -# Routing |
| 1 | +# Routing & Structure |
2 | 2 |
|
3 |
| -<h2>This module is all about routing in React Router! </h2> |
| 3 | +## Structure |
| 4 | +Before we dive into routing exercises we will first go over the basic structure of any |
| 5 | +React Router application. |
| 6 | + |
| 7 | +The minimal tree structure of a React Router application is as follows: |
| 8 | + |
| 9 | +``` |
| 10 | +|-|app/ |
| 11 | +|-|-routes.ts <-- Your route definitions go here |
| 12 | +|-|-root.tsx <-- Your app entry point |
| 13 | +|-react-router.config.ts <-- Your react-router config |
| 14 | +|-vite.config.ts <-- Your vite config |
| 15 | +``` |
| 16 | + |
| 17 | +Let's go over each of these files and what they do. |
| 18 | + |
| 19 | +### react-router.config.ts |
| 20 | + |
| 21 | +This file is used to configure the `@react-router/dev` vite plugin, which is responsible |
| 22 | +for picking up your routes from the `app/routes.ts` file and generating types for them, |
| 23 | +bundling your app and many other things. |
| 24 | + |
| 25 | +The general structure of this file is as follows: |
| 26 | + |
| 27 | +```ts |
| 28 | +import { type Config } from '@react-router/dev/config' |
| 29 | + |
| 30 | +export default { |
| 31 | + // Config options... |
| 32 | + // The directory where your app lives (this is the default) |
| 33 | + appDirectory: 'app', |
| 34 | + // If you want to change the build output directory (this is the default) |
| 35 | + buildDirectory: 'build', |
| 36 | + // Server-side render by default, to enable SPA mode set this to `false` |
| 37 | + ssr: true, |
| 38 | +} satisfies Config |
| 39 | +``` |
| 40 | + |
| 41 | +If you want to find all the config options and what they do you can find them here: |
| 42 | +📜 https://reactrouter.com/api/framework-conventions/react-router.config.ts |
| 43 | + |
| 44 | +### routes.ts |
| 45 | + |
| 46 | +This is a special file that the vite plugin from `@react-router/dev` uses to automatically |
| 47 | +register your routes by using the **default export** from this file. |
| 48 | + |
| 49 | +This file can contain any sort of JavaScript or TypeScript code, and you can use |
| 50 | +variables, functions, imports, etc. to define your routes dynamically. |
| 51 | + |
| 52 | +This file needs to live under your `app` directory, and it needs to be named `routes.ts`. |
| 53 | +The general structure of this file is as follows: |
| 54 | + |
| 55 | +```ts |
| 56 | +import { type RouteConfig } from '@react-router/dev/routes' |
| 57 | + |
| 58 | +// React Router uses this export to register your application routes |
| 59 | +export default [] satisfies RouteConfig |
| 60 | +``` |
| 61 | + |
| 62 | +Find the following documentation for more information: |
| 63 | +📜 https://reactrouter.com/api/framework-conventions/routes.ts |
| 64 | +📜 https://api.reactrouter.com/v7/interfaces/_react_router_dev.routes.RouteConfigEntry.html |
| 65 | + |
| 66 | + |
| 67 | +### root.tsx |
| 68 | + |
| 69 | +This is the entry point of your application, and it is where you will set up your |
| 70 | +general HTML layout and structure and everything that is to be used by your whole |
| 71 | +application. This file is the equivalent of your entry point in a traditional React app |
| 72 | +created with Create React App or Vite and the index.html file that it uses to attach to. |
| 73 | + |
| 74 | +This file is special to all the other kinds of route entry files because this file can |
| 75 | +contain the special `Layout` export that will be used as the root layout for your |
| 76 | +application whether it's rendering the app UI or the error UI. |
| 77 | + |
| 78 | +More information on the `root.tsx` file can be found here: |
| 79 | +📜 https://reactrouter.com/api/framework-conventions/root.tsx |
| 80 | + |
| 81 | +More information on the `Layout` export can be found here: |
| 82 | +📜 https://reactrouter.com/api/framework-conventions/root.tsx#layout-export |
| 83 | + |
| 84 | +### vite.config.ts |
| 85 | + |
| 86 | +This is the vite config file, and it is where you will set up your vite plugins |
| 87 | +and other vite specific configuration. |
| 88 | + |
| 89 | +This is where we register the `react-router` vite plugin that is responsible for |
| 90 | +unlocking the full potential of React Router framework mode in your application. |
| 91 | + |
| 92 | +Here's a minimal vite config file that registers the `@react-router/dev` plugin: |
| 93 | + |
| 94 | +```ts |
| 95 | +import { defineConfig } from 'vite' |
| 96 | +import { reactRouter } from '@react-router/dev/vite' |
| 97 | + |
| 98 | +export default defineConfig({ |
| 99 | + plugins: [reactRouter()], |
| 100 | +}) |
| 101 | +``` |
| 102 | + |
| 103 | +## Routing |
4 | 104 |
|
5 | 105 | You will learn how to set up routes, register them manually, use third party libraries to
|
6 | 106 | add custom conventions, and how you can use JavaScript to define your routes dynamically.
|
|
0 commit comments