logologo
  • AI Tools

    DB Query GeneratorMock InterviewResume BuilderLearning Path GeneratorCheatsheet GeneratorAgentic Prompt GeneratorCompany ResearchCover Letter Generator
  • XpertoAI
  • MVP Ready
  • Resources

    CertificationsTopicsExpertsCollectionsArticlesQuestionsVideosJobs
logologo

Elevate Your Coding with our comprehensive articles and niche collections.

Useful Links

  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation
  • About Us

Resources

  • Xperto-AI
  • Certifications
  • Python
  • GenAI
  • Machine Learning

Interviews

  • DSA
  • System Design
  • Design Patterns
  • Frontend System Design
  • ReactJS

Procodebase © 2024. All rights reserved.

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

React Router: Navigating Between Pages with Ease

author
Generated by
Abhishek Goyan

24/08/2024

React

Sign in to read full article

When building modern web applications with React, it’s essential to manage navigation between various components or "pages." Enter React Router, a standard library for routing in React apps that helps developers implement dynamic navigation without the hassle. This blog is meant to demystify how React Router works, guiding you through setting up routes and using them effectively.

What is React Router?

React Router is a declarative routing library for React. It lets you define routes in a single place and makes it easy to navigate between different components. Each route corresponds to a component that renders when the current URL matches its path. This approach makes it possible to have a multi-page feel within a single-page application (SPA).

Getting Started

To get up and running with React Router, you'll need to install it in your React project. If you haven't created a React app yet, you can start by running:

npx create-react-app my-app cd my-app npm install react-router-dom

Basic Setup

Once you have React Router installed, you can start defining routes. In your main App.js file, you can set up your router like so:

import React from 'react'; import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'; import Home from './Home'; import About from './About'; import NotFound from './NotFound'; function App() { return ( <Router> <div> <Switch> <Route path="/" exact component={Home} /> <Route path="/about" component={About} /> <Route component={NotFound} /> </Switch> </div> </Router> ); } export default App;

Understanding the Code

  • Router: This is the main wrapper for your routes. It enables routing capabilities.
  • Route: Each Route component matches a specific path to a component that should be rendered when the path matches.
  • Switch: The Switch component renders the first child Route that matches the path. If no paths match, it can render a fallback component, which in this case is NotFound.

Creating Components

Now, let’s create the components we referenced above.

Home.js:

import React from 'react'; function Home() { return <h1>Welcome to the Home Page</h1>; } export default Home;

About.js:

import React from 'react'; function About() { return <h1>About Us</h1>; } export default About;

NotFound.js:

import React from 'react'; function NotFound() { return <h1>404 - Not Found</h1>; } export default NotFound;

Adding Navigation Links

To navigate between these pages, we’ll need to use the Link component from React Router. Here's how you can update your App.js to include navigation links:

import React from 'react'; import { BrowserRouter as Router, Route, Switch, Link } from 'react-router-dom'; import Home from './Home'; import About from './About'; import NotFound from './NotFound'; function App() { return ( <Router> <nav> <ul> <li><Link to="/">Home</Link></li> <li><Link to="/about">About</Link></li> </ul> </nav> <Switch> <Route path="/" exact component={Home} /> <Route path="/about" component={About} /> <Route component={NotFound} /> </Switch> </Router> ); } export default App;

Nested Routes

React Router also allows for nested routing, meaning that you can have routes within routes. Let’s say you want to add more details under the "About" page. You can create a new component for "Team" details and set it up as a nested route in About.js.

Team.js:

import React from 'react'; function Team() { return <h2>Meet Our Team</h2>; } export default Team;

Now modify About.js to include the nested route:

import React from 'react'; import { Route, Link, Switch } from 'react-router-dom'; import Team from './Team'; function About() { return ( <div> <h1>About Us</h1> <nav> <ul> <li><Link to="/about/team">Our Team</Link></li> </ul> </nav> <Switch> <Route path="/about/team" component={Team} /> </Switch> </div> ); } export default About;

How It Works

Now if you run your application and navigate to the "About" page, you will see a link for "Our Team." Clicking this link will render the Team component while keeping the About header intact. This organization promotes component reuse and a clear structure within your application.

Summary of Key Features

  • Dynamic Rendering: Components render based on the current URL, which can be changed without reloading the page.
  • Declarative: Define routing rules in a straightforward, human-readable way.
  • Nested Routing: Easily manage routes within routes to create a clean and organized navigation structure.
  • History Management: React Router keeps track of your navigation history, allowing users to go back and forth seamlessly.

By leveraging React Router, developers can create intuitive user experiences that navigate seamlessly between pages while maintaining all the benefits of a single-page application.

Popular Tags

ReactReactRouterJavaScript

Share now!

Like & Bookmark!

Related Collections

  • Mastering React Concepts

    24/08/2024 | ReactJS

  • React Interview Challenges: Essential Coding Problems

    14/09/2024 | ReactJS

Related Articles

  • Forms in React: Handling User Input

    24/08/2024 | ReactJS

  • Getting Started with React Storybook and Cypress

    03/09/2024 | ReactJS

  • Creating a Search Autocomplete Typeahead Component in React

    14/09/2024 | ReactJS

  • Understanding React JS

    28/11/2024 | ReactJS

  • Lists and Keys: Rendering Multiple Items in React

    24/08/2024 | ReactJS

  • Understanding React Suspense

    16/07/2024 | ReactJS

  • Understanding React Component Lifecycles: A Deep Dive into Lifecycle Methods

    24/08/2024 | ReactJS

Popular Category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design