Routing in React JS Beginner’s Guide
Updated on 15th Dec, 23 9.1K Views

Delve into the intricacies of building intricate navigation systems, seamlessly handling user interactions, and delivering an unforgettable online experience. As you traverse the pages of this guide, prepare to ignite your passion for web development and witness your skills soar to extraordinary heights. Unleash the potential of React JS routing and become the architect of cutting-edge digital landscapes.

Table of Contents

Watch this video on our React JS full course:

What is Routing?

Routing is a fundamental concept in web development that involves determining how an application responds to different URLs or paths. In the context of a React JS application, routing allows us to display different components based on the URL. This enables users to navigate between various views without the need to refresh the entire page.

Setting Up a React Project

Before we delve into routing in React JS, let’s begin by establishing a React project. To streamline this process, we’ll employ Create React App (CRA), a widely-used tool that offers a pre-configured setup for developing React applications. Follow the steps below to create a new React project:

Install Node.js: To set up Node.js on your system, make sure it is installed, as it is necessary for React development. Access the official website of Node.js.

to obtain the Node.js installation package and complete the installation process.

Create a New React Project: Open your terminal or command prompt and run the following command to create a new React project using Create React App:

npx create-react-app my-react-app

This command makes a new directory called “my-react-app” and constructs a basic React project structure within it.

Navigate to the Project Directory: Gain access to the project directory by running the subsequent command:

cd my-react-app

Initiate the Development Server: Activate the development server using the subsequent command.

npm start

This instruction initiates the server for development and launches the application in the browser set as your default.

Learn from industry experts through our React JS Course.

Basic Routing with React Router

Basic Routing with React Router

React Router is a popular routing library for React apps. It provides a declarative way to define routes and render components based on those routes. To get started with React Router, follow these steps:

Installing React Router

To incorporate React Router into your project, access your terminal or command prompt, go to the directory where your project is located, and execute the subsequent command:

npm install react-router-dom

This command installs the React Router package and its dependencies.

Creating Routes

To establish routes in your React application, include the required components from React Router, like BrowserRouter, Route, and Switch. Within your primary application component, enclose the routes with the BrowserRouter component to enable routing capabilities. Consider the following illustration:

import { BrowserRouter, Route, Switch } from 'react-router-dom';
function App() {
  return (
    <BrowserRouter>
      <Switch>
        <Route path="/" exact component={Home} />
        <Route path="/about" component={About} />
        <Route path="/contact" component={Contact} />
      </Switch>
    </BrowserRouter>
  );
}

We define three routes in this example: “/” for the Home component, “/about” for the About component, and “/contact” for the Contact component. The “exact” keyword ensures that the Home component is only rendered when the URL exactly matches.

Rendering Components

Use the Route component and define the path and component properties to render components based on routes. When the URL matches the defined path, the Route component will render the supplied component. Here is an example of how to render the Home component:

function Home() {
  return <h1>Welcome to the Home Page!</h1>;
}

By adding the above Home component to the route configuration, it will be rendered when the URL matches “/“.

Know all the React Basics to take your first step through React JS Tutorial.

Route Parameters and Dynamic URLs

Route parameters allow us to create dynamic routes by passing variable values within the URL. This flexibility is essential for building applications that require dynamic data or user-specific content.

Passing Parameters in Routes

One of the essential features of routing in React.js is the ability to pass parameters through routes. This functionality allows us to send data along with the route URL, enabling dynamic and personalized content rendering. By utilizing route parameters, we can create versatile and flexible applications that respond to user input.

We use placeholders in the route path to pass parameters to routes. As variables, these placeholders take on various values based on how the user interacts with the application. Let’s use a blog application with individual blog articles as an example. Based on each blog post’s distinctive identifier, we wish to display the contents of each entry.

First, we define the route with a parameter placeholder:

<Route path="/blog/:postId" component={BlogPost} />

In this illustration, “:id” symbolizes a variable that can assume any value. For instance, the URLs “/user/123” and “/user/abc” are considered valid and correspond to this specific route.

Get 100% Hike!

Master Most in Demand Skills Now !

Accessing Route Parameters

Once we have passed parameters through routes, we need a way to access and utilize those parameters within our components. React Router provides various methods to access route parameters, depending on the component’s type and the specific use case.

In functional components, we can access route parameters using the useParams hook provided by React Router. Let’s continue with our previous example of the blog application:

import { useParams } from 'react-router-dom';
function BlogPost() {
  const { postId } = useParams();
  // Access the postId parameter and perform actions based on its value
  return (
    // JSX code for rendering the blog post content
  );
}

Within the provided code snippet, we make use of the useParams hook from the react-router-dom package. By employing this hook, we have the capability to retrieve the postId parameter from the route and assign it to the postId variable. Consequently, we gain the ability to employ this parameter in various ways, such as retrieving the relevant blog post data or executing any other essential actions as required.

In this instance, the “useParams” hook is employed to retrieve the value of the “id” parameter from the URL. This extracted value can subsequently be utilized within the component.

Dynamic Routing and Nested Routes

React Router allows us to create dynamic routes and handle nested routes, providing a powerful mechanism for building complex web applications with multiple levels of navigation. Dynamic routing refers to the creation of routes based on data or user input. Nesting routes involve the nesting of routes within each other to establish a hierarchical structure.

Let’s explore dynamic routing with an example. Suppose we have an e-commerce application with multiple product categories, and we want to dynamically generate routes for each category. We can achieve this by mapping over an array of categories and creating routes dynamically:

import { Route, Link } from 'react-router-dom';
function App() {
  const categories = ['electronics', 'clothing', 'books'];
  return (
    <div>
      <ul>
        {categories.map((category) => (
          <li key={category}>
            <Link to={`/products/${category}`}>{category}</Link>
          </li>
        ))}
      </ul>
      <Route path="/products/:category" component={CategoryPage} />
    </div>
  );
}
function CategoryPage() {
  const { category } = useParams();
  // Fetch products for the specified category and render them
  return (
    // JSX code for rendering the products
  );
}

In the above code, we generate route links dynamically based on the categories array. Each category link points to a dynamic route with the category parameter. For example, clicking on the “electronics” link will navigate to the URL /products/electronics, and the CategoryPage component will be rendered, fetching and displaying the relevant products.

Nested routes allow us to define routes within other routes, enabling the creation of complex page structures. This is useful when building applications with multiple levels of navigation or when organizing components into reusable hierarchies. Let’s illustrate nested routes with an example:

import { Route, Link } from 'react-router-dom';
function App() {
  return (
    <div>
      <ul>
        <li>
          <Link to="/dashboard">Dashboard</Link>
        </li>
        <li>
          <Link to="/settings">Settings</Link>
        </li>
      </ul>
      <Route path="/dashboard" component={Dashboard} />
      <Route path="/settings" component={Settings} />
    </div>
  );
}
function Dashboard() {
  return (
    <div>
      {/* JSX code for rendering the dashboard */}
      <Route path="/dashboard/profile" component={Profile} />
      <Route path="/dashboard/orders" component={Orders} />
    </div>
  );
}
function Settings() {
  return (
    <div>
      {/* JSX code for rendering the settings */}
    </div>
  );
}
function Profile() {
  return (
    <div>
      {/* JSX code for rendering the profile */}
    </div>
  );
}
function Orders() {
  return (
    <div>
      {/* JSX code for rendering the orders */}
    </div>
  );
}

In the code above, we have a simple application with a dashboard and settings page. The dashboard component contains two nested routes, /dashboard/profile and /dashboard/orders, each rendering a specific section of the dashboard. Nested routes provide a structured approach to organizing components and handling complex page layouts.

Prepare yourself for your React interview through React Interview Questions and Answers.

Navigation and Linking

Navigation and Linking

Navigation and linking are essential concepts in web development that contribute to creating dynamic and secure web applications. They are crucial for creating a seamless user experience within a web application. They allow users to move between different pages or sections of the application. Here are the key aspects to consider:

To create navigation links, you typically use anchor tags (<a>) in HTML or link components in frameworks like React or Angular. For example, in React, you can use the Link component from React Router to define navigation links that prevent full page reloads, resulting in faster navigation.

import { Link } from 'react-router-dom';
const Navigation = () => {
  return (
    <nav>
      <ul>
        <li>
          <Link to="/">Home</Link>
        </li>
        <li>
          <Link to="/about">About</Link>
        </li>
      </ul>
    </nav>
  );
};

Career Transition

Non-Tech to IT Associate | Career Transformation | AWS Certification Course - Intellipaat Reviews
Non Tech to DevOps Engineer Career Transition | Intellipaat Devops Training Reviews - Nitin
Upskilled & Got Job as Analyst After a Career Break |  Data Science Course Story - Shehzin Mulla
Successful Career Change after Completion of AWS Course - Krishnamohan | Intellipaat Review
Got Job Promotion After Completing Artificial Intelligence Course - Intellipaat Review | Gaurav
Intellipaat Reviews | Big Data Analytics Course | Career Transformation to Big Data | Gayathri

To highlight the currently active link, you can apply styling to differentiate it from other links. In React Router, you can use the NavLink component, which adds an active class to the active link. You can then use CSS to style the active link differently.

import { NavLink } from 'react-router-dom';
const Navigation = () => {
  return (
    <nav>
      <ul>
        <li>
          <NavLink exact to="/" activeClassName="active">
            Home
          </NavLink>
        </li>
        <li>
          <NavLink to="/about" activeClassName="active">
            About
          </NavLink>
        </li>
      </ul>
    </nav>
  );
};

Programmatic Navigation

Sometimes, you may need to navigate programmatically, such as after a successful form submission or upon certain conditions. In React Router, you can use the history object to programmatically navigate to a specific route.

import { useHistory } from 'react-router-dom';
const MyComponent = () => {
  const history = useHistory();
  const handleButtonClick = () => {
    // Navigate to the "/dashboard" route
    history.push('/dashboard');
  };
  return (
    <div>
      <button onClick={handleButtonClick}>Go to Dashboard</button>
    </div>
  );
};

Route Guards and Authentication

Route guards and authentication help protect certain routes or pages within a web application to ensure only authorized users can access them. Here’s what you need to know:

Implementing Route Guards

Route guards are mechanisms that allow or block navigation to specific routes based on certain conditions. In frameworks like Angular, you can use route guards like CanActivate, CanActivateChild, or CanLoad to control access to routes. These guards can perform checks, such as verifying user authentication, role-based authorization, or any custom conditions.

Protecting Routes with Authentication

To protect routes with authentication, you can implement an authentication service or utilize existing authentication libraries. For example, in a Node.js and Express application, you can use middleware to check if a user is authenticated before granting access to protected routes.

// Assuming you have already imported the necessary modules and set up the server.
// Define the middleware function for checking authentication
const isAuthenticated = (req, res, next) => {
if (req.isAuthenticated()) {
// User is authenticated, allow access to the route
return next();
}
// User is not authenticated, redirect to login page
res.redirect('/login');
};
// Define the route handlers
app.get('/dashboard', isAuthenticated, (req, res) => {
// Render the dashboard page
res.render('dashboard');
});

Route Configurations and Code Splitting

Route configurations and code splitting optimize the loading and performance of web applications, especially when dealing with larger projects. Here’s how you can leverage these techniques:

Configuring Routes

In frameworks like React Router or Angular, you can define routes and their configurations to map specific URLs to corresponding components or views. These configurations can include route parameters, query parameters, and additional metadata for customization.

// React Router example
<BrowserRouter>
  <Switch>
    <Route exact path="/" component={HomePage} />
    <Route path="/products/:id" component={ProductDetails} />
    <Route path="/about" component={AboutPage} />
    <Route component={NotFoundPage} />
  </Switch>
</BrowserRouter>

Lazy Loading Components

When dealing with larger web applications, loading all components upfront may impact the initial page load time. Code splitting allows you to split your application into smaller chunks and load them on-demand. In React, you can use the React.lazy() function and dynamic imports to achieve lazy loading.

// React lazy loading example
const Dashboard = React.lazy(() => import('./Dashboard'));
const App = () => {
  return (
    <div>
      <Suspense fallback={<div>Loading...</div>}>
        <Switch>
          <Route exact path="/" component={HomePage} />
          <Route path="/dashboard" component={Dashboard} />
          <Route component={NotFoundPage} />
        </Switch>
      </Suspense>
    </div>
  );
};

Advanced Routing Techniques

Routing is an important feature of web development because it allows users to navigate between different pages or components within a web application. There are certain advanced concepts you must be aware of, some of them are:

Query Parameters

Query parameters allow passing additional information in the URL, typically denoted by a question mark followed by key-value pairs. These parameters are often used to customize or filter the displayed content. For example, consider a product listing page that allows sorting by price or category. The URL may include query parameters like “?sort=price” or “?category=electronics,” which the program can read and utilize to produce the right content.

By extracting query parameters, developers can dynamically update the page content based on user preferences. JavaScript frameworks like React provide utilities like “URLSearchParams” to parse and access query parameters. The following code snippet demonstrates how to extract and utilize query parameters using the “URLSearchParams” API:

const params = new URLSearchParams(window.location.search);
const sortBy = params.get('sort');
const category = params.get('category');
// Use the extracted query parameters to fetch and display relevant data
fetchData(sortBy, category);

Route Transitions and Animations

Enhancing user experience during route transitions can significantly improve the overall feel and usability of a web application. By adding animations and smooth transitions, developers can provide visual cues to users when navigating between pages or components.

CSS transitions and animations can be utilized to achieve route transitions. For example, when a user clicks a link to navigate to a different page, applying a fade-in effect or sliding animation can make the transition appear seamless and engaging. JavaScript frameworks like React often provide built-in support for route transitions, such as React Router’s “TransitionGroup” component.

Here’s a simplified example of using CSS transitions for route transitions:

/* Define a transition for route changes */
.route-transition {
  transition: opacity 0.5s ease-in-out;
  opacity: 0;
}
/* Apply the transition to specific routes or components */
.route-transition-enter {
  opacity: 0;
}
.route-transition-enter-active {
  opacity: 1;
}
.route-transition-exit {
  opacity: 1;
}
.route-transition-exit-active {
  opacity: 0;
}
// Implement route transitions using React and CSS classes
import { CSSTransition } from 'react-transition-group';
<Route
  render={({ location }) => (
    <TransitionGroup>
      <CSSTransition
        key={location.key}
        classNames="route-transition"
        timeout={500}
      >
        <Switch location={location}>
          {/* Define your routes here */}
        </Switch>
      </CSSTransition>
    </TransitionGroup>
  )}
/>

Error Handling and 404 Routes

Error handling is an essential aspect of any web application. When a user navigates to a non-existent route or encounters an error, it’s crucial to provide appropriate feedback and gracefully handle the situation. A common approach is to define a dedicated 404 route that is displayed when no matching route is found.

In frameworks like React, developers can define a catch-all route that renders the 404 page component. This ensures that users are directed to a meaningful and visually appealing error page instead of encountering a generic error message. Example:

<Route path="*" component={NotFound} />

Furthermore, error handling techniques such as displaying error notifications, logging errors, and offering options for users to report issues can help improve the overall user experience.

Best Practices and Tips for React JS

By adhering to these best practices and implementing the aforementioned tips, developers can ensure efficient, scalable, and maintainable React JS applications. 

  • Component-based Architecture: React JS revolves around the concept of reusable components. It is essential to design components that are modular, self-contained, and focused on specific functionality. This allows for better organization, reusability, and maintainability of code.
  • State Management: Proper state management is critical for React JS applications. Use React’s built-in state or external state management libraries like Redux or MobX to manage and update application state. Keeping the state localized and minimizing unnecessary re-rendering improves performance.
  • Use Functional Components: React supports both class-based and functional components. However, functional components are preferred as they are easier to read, test, and optimize. They also allow the use of React’s Hooks API, which simplifies state management and side effects.
  • Avoid Direct DOM Manipulation: React works by efficiently updating the virtual DOM and then syncing it with the actual DOM. It is crucial to avoid direct DOM manipulation, as it can lead to inconsistencies and performance issues. Instead, use React’s declarative approach to update the UI.
  • JSX and Component Structure: JSX is a syntax extension for JavaScript that allows embedding HTML-like code within JavaScript. When writing JSX, follow best practices to maintain a clean and readable component structure. Avoid mixing too much logic within JSX and keep it focused on rendering UI components.
  • Code Organization: As React applications grow in complexity, maintaining a well-organized codebase becomes crucial. Combine similar components, styles, and utility functions. Consider using folders or modules to organize different parts of your application. Adopt a consistent naming convention and folder structure for better code navigation.
  • Prop Types and Type Checking: Use prop types to define the expected types for the props passed to your components. This helps catch potential bugs and provides documentation for component usage. Consider using TypeScript or Flow for static type checking to further enhance the reliability of your codebase.
  • Performance Optimization: React provides various techniques for optimizing performance. Memoization with React.memo, useCallback, and useMemo can prevent unnecessary re-renders. Use key props appropriately to help React efficiently update the DOM. Employ code splitting and lazy loading to improve initial load times.
  • Testing: Writing tests for your React components and logic is essential for maintaining code quality. Use popular testing libraries like Jest and Enzyme to write unit tests and integration tests. Test-driven development (TDD) can help catch bugs early and improve overall code stability.
  • Accessibility: Building accessible applications is crucial for inclusivity. Follow the Web Content Accessibility Guidelines (WCAG) and use semantic HTML elements. Provide alternative text for images, handle keyboard navigation, and ensure proper focus management for interactive elements.
  • Documentation and Collaboration: Maintain thorough documentation to assist developers working on the project and enable knowledge sharing. Use tools like Storybook or Styleguidist to document UI components. Collaborate effectively with team members by utilizing version control systems (e.g., Git) and project management tools (e.g., Jira).

Conclusion

Understanding and implementing routing in React.js is a fundamental skill for any beginner web developer. By grasping the concepts and techniques discussed in this beginner’s guide, you are equipped with the knowledge to navigate between different components and views within your React applications effectively.

Routing in React.js offers a robust method for developing web applications that are both dynamic and interactive. It grants the ability to construct intricate navigation systems, manage user interactions, and deliver a smooth user experience. Armed with the insights gained from this introductory manual, you are fully prepared to dive into the realm of mastering routing in React.js and elevate your expertise in web development.

Get your Web Development query solved on our Intellipaat’s Web Community.

Course Schedule

Name Date Details
Web Development Courses 24 Feb 2024(Sat-Sun) Weekend Batch
View Details
Web Development Courses 02 Mar 2024(Sat-Sun) Weekend Batch
View Details
Web Development Courses 09 Mar 2024(Sat-Sun) Weekend Batch
View Details

Leave a Reply

Your email address will not be published. Required fields are marked *

Speak to our course Advisor Now !

Related Articles

Subscribe to our newsletter

Signup for our weekly newsletter to get the latest news, updates and amazing offers delivered directly in your inbox.