--- title: Tutorial order: 2 --- # Tutorial Welcome to the tutorial! We'll be building a small, but feature-rich app that lets you keep track of your contacts. We expect it to take between 30-60m if you're following along. πŸ‘‰ **Every time you see this it means you need to do something in the app!** The rest is just there for your information and deeper understanding. Let's get to it. ## Setup If you're not going to follow along in your own app, you can skip this section We'll be using [Vite][vite] for our bundler and dev server for this tutorial. You'll need [Node.js][node] installed for the `npm` command line tool. πŸ‘‰οΈ **Open up your terminal and bootstrap a new React app with Vite:** ```sh npm create vite@latest name-of-your-project -- --template react # follow prompts cd npm install react-router-dom # always need this! npm install localforage match-sorter sort-by # only for this tutorial. npm run dev ``` You should be able to visit the URL printed in the terminal: ``` VITE v3.0.7 ready in 175 ms ➜ Local: http://127.0.0.1:5173/ ➜ Network: use --host to expose ``` We've got some pre-written CSS for this tutorial so we can stay focused on React Router. Feel free to judge it harshly or write your own πŸ˜… (We did things we normally wouldn't in CSS so that the markup in this tutorial could stay as minimal as possible.) πŸ‘‰ **Copy/Paste the tutorial CSS [found here][tutorial-css] into `src/index.css`** This tutorial will be creating, reading, searching, updating, and deleting data. A typical web app would probably be talking to an API on your web server, but we're going to use browser storage and fake some network latency to keep this focused. None of this code is relevant to React Router, so just go ahead and copy/paste it all. πŸ‘‰ **Copy/Paste the tutorial data module [found here][tutorial-data] into `src/contacts.js`** All you need in the src folder are `contacts.js`, `main.jsx`, and `index.css`. You can delete anything else (like `App.js` and `assets`, etc.). πŸ‘‰ **Delete unused files in `src/` so all you have left are these:** ``` src β”œβ”€β”€ contacts.js β”œβ”€β”€ index.css └── main.jsx ``` If your app is running, it might blow up momentarily, just keep going πŸ˜‹. And with that, we're ready to get started! ## Adding a Router First thing to do is create a [Browser Router][createbrowserrouter] and configure our first route. This will enable client side routing for our web app. The `main.jsx` file is the entry point. Open it up and we'll put React Router on the page. πŸ‘‰ **Create and render a [browser router][createbrowserrouter] in `main.jsx`** ```jsx lines=[3-6,9-14,18] filename=src/main.jsx import * as React from "react"; import * as ReactDOM from "react-dom/client"; import { createBrowserRouter, RouterProvider, } from "react-router-dom"; import "./index.css"; const router = createBrowserRouter([ { path: "/", element:
Hello world!
, }, ]); ReactDOM.createRoot(document.getElementById("root")).render( ); ``` This first route is what we often call the "root route" since the rest of our routes will render inside of it. It will serve as the root layout of the UI, we'll have nested layouts as we get farther along. ## The Root Route Let's add the global layout for this app. πŸ‘‰ **Create `src/routes` and `src/routes/root.jsx`** ```sh mkdir src/routes touch src/routes/root.jsx ``` (If you don't want to be a command line nerd, use your editor instead of those commands πŸ€“) πŸ‘‰ **Create the root layout component** ```jsx filename=src/routes/root.jsx export default function Root() { return ( <>