Routing | Mastering React: An In-Depth Zero to Hero Video Series

Web Tech Talk
5 Apr 202306:50

Summary

TLDRIn this React JS tutorial, the instructor introduces routing in single-page applications, explaining its necessity for navigating without page refreshes. The video contrasts traditional multi-page navigation with React's dynamic content rendering and then demonstrates how to implement routing using the 'react-router-dom' library. The instructor guides viewers through setting up routes, linking navigation, and passing parameters. The session concludes with a challenge to create an app routing to user detail pages, hinting at advanced routing topics for future videos.

Takeaways

  • πŸŽ“ This video is part of a 'React JS - Zero to Hero' series aimed at beginners learning React JS from scratch.
  • πŸ“ The previous video focused on form handling in React, while this video tackles the topic of routing in React applications.
  • 🌐 Routing is crucial for single-page applications (SPAs) as it directs users to different pages based on their actions or requests without full page reloads.
  • πŸ”— The presenter demonstrates the difference between traditional multi-page applications and SPAs, highlighting how SPAs do not refresh the page but initially lack URL changes and browser history functionality.
  • πŸ› οΈ To implement routing in React, third-party libraries like 'react-router-dom' are used, as React does not have a built-in routing solution.
  • πŸ›‘ The video shows how to install 'react-router-dom' and use it to wrap components within a 'BrowserRouter' for basic routing functionality.
  • πŸ”„ The 'Routes' and 'Route' components from 'react-router-dom' are used to define paths and associate them with corresponding components.
  • πŸ”‘ The video explains how to handle initial page loads by setting a default route and how to create navigation links using the 'Link' component.
  • 🚧 Advanced routing concepts like route parameters, nested routes, protected routes, and routing with query parameters are mentioned but will be covered in future videos.
  • πŸ“Œ A practical task is given to the viewers to create an application that routes to a user detail page based on a user ID, hinting at the use of hardcoded JSON data for user information.

Q & A

  • What is the main topic of discussion in this React JS tutorial video?

    -The main topic of discussion in this React JS tutorial video is Routing in React, specifically for single page applications.

  • Why is routing important in single page applications?

    -Routing is important in single page applications because it allows users to be directed to different pages based on their actions or requests without refreshing the page, and it enables the use of browser history features like back and forward buttons.

  • How does the tutorial demonstrate the need for routing in React applications?

    -The tutorial demonstrates the need for routing by comparing a simple HTML application with multiple HTML files to a React application that uses state, props, and conditional rendering without routing, highlighting the lack of URL changes and browser history functionality in the latter.

  • What is the difference between routing in Angular and React as mentioned in the video?

    -In Angular, routing comes as an inbuilt module, whereas in React, developers can either implement their own routing solution or use third-party libraries like React Router.

  • Which library does the tutorial use to implement routing in a React application?

    -The tutorial uses the 'react-router-dom' library to implement routing in a React application.

  • How does the tutorial suggest adding routing to a React application?

    -The tutorial suggests installing the 'react-router-dom' library, wrapping components inside the 'BrowserRouter', creating 'Route' elements with 'path' and 'element' props, and using 'Link' components for navigation.

  • What is the purpose of the 'path' and 'element' props in a 'Route' component?

    -The 'path' prop in a 'Route' component is used to match the URL, and the 'element' prop specifies the component that should be rendered when the path matches the URL.

  • How can you make a React application show a default component when the application is loaded?

    -To show a default component when a React application is loaded, you can add a 'Route' with an empty path ('/') that renders the desired default component.

  • What does the tutorial suggest to fix non-working navigation links in a React application?

    -To fix non-working navigation links in a React application, the tutorial suggests using the 'Link' component from 'react-router-dom' and passing the 'to' prop with the exact path name.

  • What advanced routing concepts does the tutorial mention will be covered in future videos?

    -The tutorial mentions that future videos will cover advanced routing concepts such as route with params, nested routes, protected routes, and routing with query params.

  • What task does the tutorial assign to the viewers to practice routing in React?

    -The tutorial assigns a task to create an application where clicking on a user routes to a user detail page, fetching and displaying information related to that user using a hardcoded JSON and the user ID passed through the route.

Outlines

00:00

πŸ“š Introduction to React Routing

This paragraph introduces the topic of routing in React for beginners. It explains the importance of routing in single-page applications (SPAs) and how it directs users to different pages based on their actions or requests. The instructor provides a comparison between a traditional multi-page HTML application and a React application to illustrate the need for routing. They show that without proper routing, the URL does not change, and browser navigation buttons do not work as expected. The paragraph concludes with an introduction to implementing routing using the 'react-router-dom' library, which is a third-party library for handling navigation in React applications.

05:02

πŸ›  Implementing Basic Routing in React

The instructor demonstrates how to implement basic routing in a React application. They guide through the process of installing the 'react-router-dom' library and setting up the router in the application. The explanation includes wrapping components within the 'BrowserRouter', creating route elements with 'path' and 'element' props, and configuring the routes to display different components based on the URL. The paragraph also covers how to handle the initial page load by setting a default route and how to integrate navigation links using the 'Link' component from 'react-router-dom'. The summary emphasizes the simplicity of setting up basic routing in React applications.

πŸ” Advanced Routing Concepts in React

In this paragraph, the instructor briefly touches on advanced routing concepts such as route parameters, nested routes, protected routes, and routing with query parameters. They mention that these topics will be covered in a separate video series called 'React JS Hero to Superhero'. The focus then shifts to demonstrating how to pass parameters through routes using a colon syntax. An example is given where an 'id' parameter is passed in the URL, and the 'useParams' hook is used in the 'About' component to access this parameter. The paragraph ends with a task for the viewers to create an application that routes to a user detail page based on a user ID, suggesting the use of a hardcoded JSON for demonstration purposes.

Mindmap

Keywords

πŸ’‘React JS

React JS is a popular open-source JavaScript library used for building user interfaces, particularly for single-page applications. It is maintained by Facebook and a community of individual developers and companies. In the video, React JS is the main technology being taught, with the series aimed at beginners who want to learn to build applications from scratch.

πŸ’‘Routing

Routing in the context of web development refers to the process of directing users to different pages or views within an application based on their actions or requests. In the video, routing is a key topic discussed, especially for single-page applications, where it allows for changing the content dynamically without refreshing the entire page.

πŸ’‘Single Page Application (SPA)

A Single Page Application is a web application that loads a single HTML page and dynamically updates that page as the user interacts with the app. In the video, the contrast between traditional multi-page applications and SPAs is highlighted, emphasizing the importance of routing in SPAs to provide a seamless user experience without page reloads.

πŸ’‘React Router DOM

React Router DOM is a standard library used for routing in React applications. It allows developers to create a more flexible and user-friendly navigation system within their applications. In the video, the instructor demonstrates how to use React Router DOM to implement routing in a React application.

πŸ’‘Components

In React, a component is a reusable piece of code that can be used to build the user interface. Components can be class-based or functional, and they can manage their own state or props. The video script mentions components like the 'Header' and 'App', which are used to structure the application and implement routing.

πŸ’‘State and Props

State and props are fundamental to React for managing and passing data within components. State is an object that holds information about the component, while props are the values passed from a parent component to a child component. The video discusses how state and props can be used to control the visibility of content, although it points out that this alone is not sufficient for proper routing.

πŸ’‘Conditional Rendering

Conditional rendering in React is a technique used to render different components or elements based on certain conditions. It is a way to control what is displayed on the screen. The video script uses conditional rendering as an example of how content can be shown or hidden without changing the URL or refreshing the page.

πŸ’‘URL Parameters

URL parameters are values that are passed along with the URL in a web application. They can be used to pass data between pages or to identify specific resources. In the video, the concept of passing parameters through routes is introduced, showing how to use them to pass an 'id' parameter to a component.

πŸ’‘Link Component

The Link component from React Router DOM is used to create links that navigate to different routes within the application. It replaces the traditional anchor tag and is used to handle navigation without a page refresh. The video demonstrates how to use the Link component to create navigation links that change the URL when clicked.

πŸ’‘useParams Hook

The useParams hook from React Router DOM is used to access the parameters from the URL in a functional component. It allows developers to extract and use URL parameters within their components. In the video, useParams is used to retrieve an 'id' parameter from the URL and display it in the 'About' component.

πŸ’‘Hardcoded JSON

A hardcoded JSON is a static representation of data in the form of a JSON object, often used for demonstration or testing purposes. In the video, the instructor suggests using a hardcoded JSON to simulate a list of users and fetch user details based on an 'id' passed through the route.

Highlights

Introduction to React JS Zero to Hero series for beginners.

Explanation of form handling in the previous video.

Introduction to the topic of routing in React.

Definition of routing in single page applications.

Demonstration of a simple HTML application with multiple pages.

Observation of page refresh and URL changes in a non-SPA.

Comparison with a React application using state and props for content display.

Illustration of the need for routing in SPAs with an example using React Router.

Explanation of how routing allows for URL changes and functional back/forward buttons.

Overview of implementing routing in React using react-router-dom.

Instructions on installing react-router-dom using npm.

Code walkthrough of wrapping components in BrowserRouter.

Step-by-step guide to creating routes with path and element props.

Solution to displaying content on application load with a default route.

Integration of Link components from react-router-dom for navigation.

Introduction to advanced routing concepts like route with params, nested routes, and protected routes.

Demonstration of passing parameters through routes using colon syntax.

Tutorial on fetching and displaying user information based on user ID.

Assignment for viewers to create an application routing to user detail pages.

Encouragement to use a hardcoded JSON for user data in the exercise.

Announcement of the next video, which will involve creating a complete application.

Call to action for viewers to like, subscribe, and support the channel.

Transcripts

play00:11

Hi Friends

play00:12

Welcome back to React JS - Zero to Hero series.

play00:15

This series is for beginners who wants to learn React JS from Scratch.

play00:18

In the last video I have explained about form handling in react.

play00:22

In this video, I am going to explain about Routing in React.

play00:25

Let's start.

play00:27

When comes to single page applications, routing is an important topic.

play00:30

Routing is a process in which a user is directed to different pages based on their action or

play00:36

request.

play00:37

I would like to show you an example, so that you can easily understand the need for routing

play00:41

in single page applications.

play00:43

I have created this simple HTML application, in which I have 3 pages.

play00:47

This is not a single page application and so you can see we have 3 html files.

play00:52

Let me open this in browser.

play00:54

And, when I click any of the navigation links, we can see the entire html is getting changed.

play01:00

Also, every time the page is getting refreshed.

play01:03

And, we can also see the back and forward buttons are working.

play01:08

Ok, let me show you another application.

play01:10

This is a react application.

play01:12

Here I have tried to replicate the same usecase.

play01:15

But, I am just hiding and showing different contents using state, props and conditional

play01:20

rendering.

play01:22

This time, we can see the page is not refreshing.

play01:24

But we can also see, the url is not getting changed and so back and forward buttons are

play01:29

not working.

play01:30

So, this is not the right solution.

play01:33

Here comes the need for routing.

play01:35

Let me show you this one.

play01:37

This one is implemented using router.

play01:40

And so, we can see the page is not refreshing, in the mean time, the URL is getting changed

play01:47

and the back and forward buttons are also working.

play01:49

This is why we need routing for single page applications.

play01:52

Now, let me show you how we can implement routing in react applications.

play01:56

In frameworks like angular, routing is coming as an inbuilt module.

play02:01

But in react, either we can implement our own routing solution or we can use third party

play02:05

libraries like react router.

play02:07

For this demo, I am going to use this react router dom library.

play02:12

Ok.

play02:13

Let's start.

play02:14

I have actually created the same application but in this version, I have not implemented

play02:19

routing so far.

play02:20

And so, we can see the navigation links are not working.

play02:23

Let me explain the code setup quickly.

play02:26

In index dot js, I am having a header component and app component.

play02:30

In header component, we can see just the navigation items.

play02:34

And in app dot js, I am just having the About component.

play02:37

So, we can see this, but the links are not working.

play02:41

Let's add routing.

play02:43

First, we need to install the react router dom library.

play02:46

We can use npm install react router dom to install this library.

play02:51

It is installed.

play02:53

Now, let me start the server and let me go to index dot js.

play02:58

Here, let me wrap these two components inside browser router.

play03:06

We can import browser router from react router dom library.

play03:10

And in app component, first let me delete this hardcoded about component.

play03:14

And, we are going to create the routes.

play03:17

For that, first let's have a routes element.

play03:19

Inside that, let me have a route element.

play03:23

We can also import routes and route from the react router dom library.

play03:26

To this route component, we can pass path and element props.

play03:31

Path is the one which is going to get matched with the URL.

play03:35

Element is the one where we need to mention the component.

play03:39

And so, when we enter an URL, based on the matched path, the respective component will

play03:44

be served.

play03:45

So, when it is going to be about, let me show the About component, similarly let me create

play03:50

two more routes and configure the respective path and element.

play03:54

So, if I change the URL, we can see the respective content.

play04:05

But, initially when the application is loaded, nothing is showing.

play04:10

To fix that, we can add another route.

play04:13

When it is going to be an empty path, we can show the about component.

play04:16

Like this.

play04:17

Now, if I load the application, we can see the about page.

play04:22

Ok, but still the links are not working, because we haven't added any code for that.

play04:27

So, let me go to the header component and here I can use the link.

play04:31

We can import the link from react router dom library.

play04:36

To this link component, I can pass the to props.

play04:40

I can have the exact path name so that when this link is clicked, url will be changed.

play04:46

Let me also use link in the other two navigation items.

play04:55

So now, we can see our navigation is working.

play04:58

This is how we can implement routing in react applications.

play05:01

Actually, routing is a big topic.

play05:04

There are many concepts in routing like route with params, nested routes, protected routes,

play05:10

routing with query params, etc.

play05:12

I need to cover each topic in a separate video.

play05:15

I will do that as part of advanced concept series.

play05:17

React JS Hero to Superhero.

play05:20

But for now, let me quickly show you how we can pass params through route.

play05:24

For that, let me add one more route.

play05:27

For passing params, we can use colon.

play05:29

I am going to pass an id param.

play05:32

So, I can mention like this.

play05:34

Now in header component, I am going to hardcode an id in the url.

play05:39

And in about component, I can use useParams and destructure the id from the URL.

play05:44

And for now, let me show that id here.

play05:49

And so, when I navigate to about route, we can see the id we passed is showing here.

play05:55

Hope you understood.

play05:56

Ok.

play05:57

Let me give you a task.

play05:59

You can create an application similar to this.

play06:01

When you click any user here, you need to route to user detail page.

play06:05

In which, you can get the user id and fetch the information related to that particular

play06:09

user and show that in the screen.

play06:12

Like this.

play06:13

You can use a hardcoded json to have some users and use the user id to fetch the particular

play06:18

user.

play06:19

Try this, if you need the solution code, you can get it from this repo.

play06:23

The link is also available in the description.

play06:26

And so, we have covered all the basic concepts in React.

play06:29

In the next video, using all these concepts we are going to create a complete application.

play06:34

That's all for today.

play06:35

Please like this video.

play06:38

Subscribe to my channel and support me.

play06:40

Thank you.

play06:43

Bye.

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
React JSRoutingSingle Page AppsWeb DevelopmentTutorialBeginnersFrontendState PropsConditional RenderingReact Router