Full React Tutorial #21 - The React Router
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: React
View skill →Related Reads
📰
📰
📰
📰
I Found a Surprisingly Fun Way to Practice Frontend Development
Dev.to AI
The Enter key that submits your form while a Japanese user is still typing
Dev.to · greymoth
The two-Reacts bug: when packages aren't singletons
Dev.to · r9v
🚀 Introducing Prism Guard — An Open Source Frontend Architecture Intelligence Platform
Dev.to · Ritumoni Sarma
🎓
Tutor Explanation
DeepCamp AI