Skip to content

Commit f785e97

Browse files
committed
wrap up the routing section and add proper docs
1 parent a3ffb55 commit f785e97

File tree

8 files changed

+198
-35
lines changed

8 files changed

+198
-35
lines changed

exercises/01.routing/01.problem.routing/README.mdx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,18 @@ In this exercise, you will:
1313

1414
## Getting Started
1515

16-
To get started, find our helpful assistants comments in the `app/routes.ts` file. (They include 🐨 emoji)
16+
To get started, find our helpful assistants comments in the `app/routes.ts` file. (They include 🐨 emoji)
17+
and implement the following routes:
18+
- `Landing page layout` that wraps all the other routes (`./routes/_landing.tsx`).
19+
- `/` - The index route that renders the landing page (`./routes/_landing._index/route.tsx`).
20+
- `/about` - The about route that renders the `About` page (`./routes/_landing.about.tsx`).
21+
- `/products` - The products route that renders the `Products` page (`./routes/_landing.products.tsx`).
22+
- `/products/:productId` - The product details route that renders the `ProductDetails` page (`./routes/_landing.products.$productId.tsx`).
23+
- `/contact` - The contact route that renders the `Contact` page (`./routes/_landing.contact.tsx`).
24+
- `/cart` - The cart route that renders the `Cart` page (`./routes/_landing.cart.tsx`).
25+
- `/terms-of-use` - The terms of use route that renders the `TermsOfUse` page (`./routes/_landing.terms-of-use.tsx`).
26+
- `/terms-of-service` - The terms of service route that renders the `TermsOfService` page (`./routes/_landing.terms-of-service.tsx`).
27+
28+
> Bonus (only after completing the above): Create a utility function to easily define landing page routes.
29+
> This utility should be used to define the routes above
30+
Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
import {
22
type RouteConfig,
3+
// 💰 Use these imports already prepared for you below!
34
route,
45
index,
56
layout,
67
} from '@react-router/dev/routes'
7-
/**
8-
* 🐨 Create your route configuration here.
9-
* 🐨 This file is used to define the structure of your application routes.
10-
*
11-
* 🐨 You need to add the following routes:
12-
* - Home page ("/")
13-
* - About page ("/about")
14-
* - Contact page ("/contact")
15-
* - Products page ("/products")
16-
* - Product details page ("/products/:productId")
17-
* - Terms of Use page ("/terms-of-use")
18-
* - Terms of Service page ("/terms-of-service")
19-
*
20-
* 🐨 The files are already ready for you in the `app/routes` directory!
21-
*
22-
* Bonus points: Create a utility to define landing page routes
23-
* and use it to create the routes for the landing page.
24-
*/
8+
9+
// 🐨 Create your route configuration here.
10+
// 🐨 This file is used to define the structure of your application routes.
11+
//
12+
// 🐨 You need to add the following routes:
13+
// - Home page ("/")
14+
// - About page ("/about")
15+
// - Contact page ("/contact")
16+
// - Products page ("/products")
17+
// - Product details page ("/products/:productId")
18+
// - Terms of Use page ("/terms-of-use")
19+
// - Terms of Service page ("/terms-of-service")
20+
//
21+
// 🐨 The files are already ready for you in the `app/routes` directory!
22+
//
23+
// Bonus points: Create a utility to define landing page routes
24+
// and use it to create the routes for the landing page.
25+
2526
export default [] satisfies RouteConfig
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
# Registering Routes in React Router
22

3-
Congratulations on reaching the first exercise of the React Router Fundamentals workshop! 🎉
3+
You just registered your first routes in a react-router framework mode project! 🎉
44

5-
In this exercise, you have registered your first routes in a React Router application.
5+
In this exercise you learned how to use the `routes.ts` file to manually register your routes,
6+
and you also learned how to create a utility function to make it easier to register routes
7+
that share a common layout.
8+
9+
Even though I'm proud of you for completing this exercise, 👨‍💼 Peter the manager isn't! He
10+
thinks this approach of manually registering routes is too tedious and time consuming. He wants you
11+
to find a better way to register routes in the next exercise. So let's move on to the
12+
next exercise where you will learn how to use a third party
13+
library to automatically detect and register your routes for you!
614

715
Take a breather, and let's continue once you're ready! 🚀
16+

exercises/01.routing/02.problem.automatic-routing/README.mdx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
# Using automatic route detection
22

3+
👨‍💼 Peter doesn't want us spending our precious development time doing tedious
4+
manual work. He wants us to find a better way to register routes in our application and that
5+
is exactly what we are going to do in this exercise!
6+
37
Now that we understand how to define routes and use them effectively, let's use a package
4-
from the React Router team to automatically detect and register routes for us!
8+
from the React Router team to automatically detect and register routes for us so we can focus
9+
on building features instead of boilerplate.
510

611
## Exercise Overview
712

@@ -13,3 +18,15 @@ In this exercise, you will:
1318
## Getting Started
1419

1520
To get started, find our helpful assistants comments in the `app/routes.ts` file. (They include 🐨 emoji)
21+
22+
You will need to install a new package to help you with automatic route detection:
23+
24+
```sh
25+
npm install @react-router/fs-routes
26+
```
27+
28+
Then, implement the following steps in the `app/routes.ts` file:
29+
30+
1. Import the `flatRoutes` function from `@react-router/fs-routes`.
31+
2. Use the `flatRoutes` function to automatically detect and register all the routes in the
32+
`app/routes` directory.

exercises/01.routing/02.problem.automatic-routing/app/routes.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,23 @@ import {
44
route,
55
type RouteConfig,
66
} from '@react-router/dev/routes'
7-
/**
8-
* 🐨 As we've learned in the previous exercise this file is used to define your routes,
9-
* but wouldn't it be neat if we didn't have to manually define every new route?
10-
*
11-
* 🐨 Well, that is the point of this exercise! We will add a plugin from react-router that
12-
* automatically picks up new routes and doesn't require us to hardcode them in this file.
13-
*
14-
* 💰 First install the dev dependency into package.json called @react-router/fs-routes
15-
*/
7+
8+
// 🐨 As we've learned in the previous exercise this file is used to define your routes,
9+
// but wouldn't it be neat if we didn't have to manually define every new route?
10+
11+
// 🐨 Well, that is the point of this exercise! We will add a plugin from react-router that
12+
// automatically picks up new routes and doesn't require us to hardcode them in this file.
13+
14+
// 💰 First install the dev dependency into package.json called @react-router/fs-routes by running:
15+
// npm install -D @react-router/fs-routes
16+
17+
// 💰 Then you can import the `flatRoutes` function from the package like so:
18+
// import { flatRoutes } from '@react-router/fs-routes'
19+
20+
// 💰 Finally you can use the `flatRoutes` function to automatically detect and register
21+
// your routes like so:
22+
// export default flatRoutes() satisfies RouteConfig
23+
1624
export default [
1725
layout('./routes/_landing.tsx', [
1826
index('./routes/_landing._index/route.tsx'),

exercises/01.routing/02.solution.automatic-routing/README.mdx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22

33
Congratulations! You're successfully using automatic route detection in your React Router application! 🎉
44

5-
In this exercise, you have installed the `@react-router/fs-routes` package and registered your routes in the `app/routes.ts` file.
5+
In this exercise, you have installed the `@react-router/fs-routes` package and registered your
6+
routes in the `app/routes.ts` file.
67

8+
From now on, any route you add to the `app/routes` directory will be automatically detected
9+
and registered in your application, saving you time and effort.
10+
11+
12+
## Additional considerations
13+
14+
The package we used only supports a single layer of nested routes. If you need more complex
15+
nesting, you can use the `remix-flat-routes` package instead, which supports deeper nesting,
16+
and don't let the name fool you! It works perfectly fine with React Router framework mode projects.
17+
18+
Find it here: 📜 https://github.com/kiliman/remix-flat-routes
19+
720
Take a breather, and let's continue once you're ready! 🚀

exercises/01.routing/FINISHED.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Routing
22

3-
<h2>You just finished your first exercise! 🎉 </h2>
3+
<h2>You just learned all about routing and structure in React Router! 🎉 </h2>
4+
45
Let me know what you think in the form on the right! 👉
56

67
Done? Let's continue to the next exercise! 🚀

exercises/01.routing/README.mdx

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,106 @@
1-
# Routing
1+
# Routing & Structure
22

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
4104

5105
You will learn how to set up routes, register them manually, use third party libraries to
6106
add custom conventions, and how you can use JavaScript to define your routes dynamically.

0 commit comments

Comments
 (0)