Full React Tutorial #21 - The React Router

Net Ninja · Beginner ·🌐 Frontend Engineering ·5y ago
Skills: React90%

Key Takeaways

Sets up the React Router to handle multiple pages in a React application

Full Transcript

all right gang so far our application only has one single page we don't navigate around to other pages we just have this single home page and most websites you create are gonna have probably more than one page so we need a way to introduce multiple different pages or routes in our react application and the way we do this in react is with the react router but before we talk about how that works let me refresh you about how a typical multi-page website works so then how does a typical website that's not using react handle multiple pages well we generally always start in the browser by typing a url in the address bar and hitting enter and that sends a request to a server for that address and the server handles it the server is generally going to send back a full html page which we then view in a browser now if a user was to click on a link in that website to another page like the contact page it then sends a brand new request to the server and then the server responds by sending back a new contact html page and we view that in the browser and the cycle would continue over and over and over as we click other page links in the website so we're constantly making requests to the server for new pages now react applications don't work like this they delegate all the routing and changing of page content to the browser only and it starts the same way we make an initial request in the browser the server then responds to that by sending back the html page to the browser but it also sends back our compiled react javascript files which controls our react application so from this point on react and the react router can take full control of the application so initially the html page that we get back is virtually empty and then react injects the content dynamically using the components that we create if we then decide to click on a link to a new page the react router is going to step in and intercept that request to stop it from going to the server and instead it's going to look at the new page request and inject the required content on the screen for example clicking on a contact link the react router will tell react to inject the contact component into the browser if we were to click on an about link it would tell react to inject the about component and so forth so this is generally the way the react router works we assign a top level component for each route or page and that component is dynamically injected into the browser when we visit that route now this whole process means that we're making less requests to the server and the whole website therefore feels faster and slicker so now we know from a bird's eye perspective how this works let's see how to set it up in our code so the first thing we need to do is install the react router package because it's not a part of the core react library now to do this we need npm and we need a new terminal so click on this and then we're going to type npm install and then it's react hyphen router hyphen dom and then i'm gonna use a specific version and that version is five and to do that i'm gonna say at five now this version is currently stable version 6 is in beta but that might change over the coming weeks and months so i didn't want to use that version in this series in case it does change but i will be doing a react router 6 tutorial once a stable release is here or at least once we have a release candidate but to follow along with this use version 5 like me and then you can always upgrade to version 6 in the future so press enter to install this and then once that's installed we can close this and just go to your package.json file and you should see now the react router dom package right here so version 5.2 all right then so remember when we install something it goes inside the node modules folder so if you're wondering where it's put that it's inside here so now we've installed that package how do we actually use it and set up routing for our application well the first thing to do is to go to the root component which is this app.js file and we need to import a few things from the react router package right here so let's do that first i'm going to import and then we're going to destructure a few things the first thing we need is the browser router and we're going to say as router and that means we can use the browser router that we're importing using this name inside this file so that's the first thing we want to import and we also want to import something called the route component and also the switch components so we'll see what these do as we go forward but for now let's say from react hyphen router hyphen dom okay so now we need to surround our entire application with the router component and what that means is that we can use the router in the entire application all components that are nested inside this app component have access to the router so let us now do that i'm going to surround this div with this router component so router like so and then we get the closing tag here let's place that at the end and scoot this in alright so that's the first step the next step is to decide where we want our page content to go when we go to different pages well i want it to go inside this div right here with the class of content so i'm going to delete that home component right here and i'm going to replace it with the switch component like so now this switch component makes sure that only one route shows at any one time now we're going to talk a little bit more later about how that works but just know for now all of our routes go inside this switch component okay so now we need to set up our individual routes so what we do is we create a route for each page that we have using this route component right here now we only have one page for now so we're just going to place one route inside this switch component but later on we're going to have other pages and more routes inside here as well so let's do our route for the home page so the route component like so and then let's get the closing tag as well and then we need to add on a property to the route component which is going to be the path now the path property oops that needs to be inside the route component let's cut it and paste it right here the path is basically the route so for the home page it would just be forward slash right if we were doing an about page it could be forward slash about if it was a contact page it could be forward slash contact basically this is the path after the route of our website so if our website was called the netninja.com or credit uk it would be the path after that part of the url okay so this is the path for the home page and what we need to do is nest the component inside this route that we want to show when a user visits this route so i want to show the home component so home like so and now what we're seeing is okay i want you to show the home component right here inside the content div when we visit just forward slash now notice the nav bar is always going to show because it's not inside this switch statement this is here for every single route it doesn't matter what it is but this content inside the switch is going to change dependent on the route as we build up more routes so let me just save this for now and let's preview this now nothing really should change because we're still using just the same home page but at least it does work just forward slash like so and we get the home page so next up we're going to add another route and talk a little bit more about this switch component that we've used right here

Original Description

In this React tutorial we'll talk about the React Router & see how to set it up so that we can have multiple 'pages' on our React websites. 🐱‍💻 🐱‍💻 Course Files: + https://github.com/iamshaunjp/Complete-React-Tutorial 🐱‍👤🐱‍👤 JOIN THE GANG - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join 🐱‍💻 🐱‍💻 My Udemy Courses: + Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript + Vue JS & Firebase - https://www.thenetninja.co.uk/udemy/vue-and-firebase + D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 🐱‍💻 Helpful Links: + HTML & CSS Course - https://www.youtube.com/watch?v=hu-q2zYwEYs&list=PL4cUxeGkcC9ivBf_eKCPIAYXWzLlPAm6G + Modern JavaScript course - https://www.youtube.com/watch?v=iWOYAxlnaww&list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc + Get VS Code - https://code.visualstudio.com/ 🐱‍💻 🐱‍💻 Social Links: Facebook - https://www.facebook.com/thenetninjauk Twitter - https://twitter.com/thenetninjauk Instagram - https://www.instagram.com/thenetninja/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related Reads

📰
I Found a Surprisingly Fun Way to Practice Frontend Development
Practice frontend development in a fun way by building football-themed applications on VibeCode Arena
Dev.to AI
📰
The Enter key that submits your form while a Japanese user is still typing
Learn how to prevent the Enter key from submitting a form while a Japanese user is still typing, and why it matters for user experience
Dev.to · greymoth
📰
The two-Reacts bug: when packages aren't singletons
Learn to fix the two-Reacts bug by understanding how to handle non-singleton packages in React applications
Dev.to · r9v
📰
🚀 Introducing Prism Guard — An Open Source Frontend Architecture Intelligence Platform
Learn about Prism Guard, an open-source frontend architecture intelligence platform, and how it can help improve codebase quality
Dev.to · Ritumoni Sarma
Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →