Routing in react-js application

Tech Lead & Architect | 13+ Years in Cloud, Backend, and AI - Experienced software engineer with expertise in Java, Spring Boot, Microservices, Angular, React, Kafka, DevOps, Python, PySpark, Databricks, and Generative AI. Certified in TOGAF, AWS, and Google Cloud. Passionate about building scalable, secure, and high-performance systems. Enthusiast in Data Engineering & Agentic AI. Author of 1,200+ technical articles sharing insights across diverse tech stacks.
Date: 2022-10-11
Understanding Routing in React Applications: A Comprehensive Guide
React, a popular JavaScript library for building user interfaces, excels at creating reusable components. However, building complex applications requires a mechanism to navigate between different parts of the application, presenting different information to the user based on their interaction. This is where routing comes in. Routing, in the context of web applications, is the process of determining which code handles a given user request – in this case, a user's navigation within a single-page application (SPA) built with React. This guide will explore how routing works in React applications, using the React Router library.
Unlike traditional web applications where each page is a separate HTML file, React applications often operate as single-page applications. This means all the application's content is loaded once, and the user interface updates dynamically as the user interacts. To create this seamless experience, routing allows the application to display different content based on the URL in the browser address bar. This dynamic updating mimics the behavior of traditional multi-page applications without the overhead of loading new pages for each navigation.
The React Router library is a crucial component for implementing routing in React. It provides a declarative way to define routes, allowing developers to specify which components should be rendered based on the current URL. Essentially, it acts as a map, connecting specific URLs to specific parts of the application's code. For example, a route might be defined to display a "Home" component when the user is at the root URL ("/") and an "About" component when the user navigates to "/about".
Setting up a React application with routing involves several steps. First, you need to install Node.js and npm (Node Package Manager) on your system. Node.js is a JavaScript runtime environment, essential for running React applications. npm is a package manager that allows you to easily download and install the required libraries. The installation process typically involves downloading an installer from the Node.js website and running it. After successful installation, you can verify the installation by opening a command prompt or terminal and typing node -v and npm -v, which should display the installed versions of Node.js and npm respectively.
Next, you'll create a new React project using the Create React App (CRA) tool. CRA simplifies the project setup, providing a pre-configured environment. This is done via the command line using npx create-react-app routing-app, where 'routing-app' is the desired name for your project. This command downloads the necessary React packages and creates the basic project structure.
Once the project is created, you need to install the React Router library. This is done using npm again, with the command npm install react-router-dom. The 'react-router-dom' package specifically provides the necessary components for routing in web applications. After installation, the package.json file will list 'react-router-dom' as a dependency.
The core components of React Router are BrowserRouter, Routes, and Route. BrowserRouter is a higher-order component that wraps your main application component. It provides the context for routing, allowing other routing components to function correctly. Routes is a container that holds the various route definitions. Finally, individual Route components define specific routes; each associating a URL path with a React component to render.
For example, you might have a Home.js component, an About.js component, and a PageNotFound.js component. Each component renders a specific part of your application's interface. Inside your main application component (often named App.js), you would use the Routes component to define these routes: a route for "/" linking to Home, a route for "/about" linking to About, and a catch-all route to handle any other URL, redirecting to the PageNotFound component, effectively handling 404 errors.
To make these components accessible throughout the application, the BrowserRouter component wraps the Routes component in the application's main file (index.js). This ensures the routing context is available to all the components within the application.
The implementation involves importing these components (BrowserRouter, Routes, Route) from react-router-dom into your App.js file. Then, within the App.js component, you define the routes using the <Route> component and specify the path and component. The catch-all route, usually placed last in the Routes component, will handle all other URLs that don't match the previously defined routes.
Finally, to run the application, navigate to the project directory in the terminal and execute the command npm start. This will start the development server and open the application in your default browser. The application will then dynamically render the appropriate components based on the URL, providing a seamless navigation experience within the single-page application. The user interacts by clicking links or typing URLs into the browser address bar, prompting the application to switch between the components as the URL changes.
In essence, routing in React, facilitated by the React Router library, allows developers to build complex, interactive applications with a single HTML page. The library provides a clean, declarative way to map URLs to different components, making it easy to manage navigation and create user-friendly, dynamic interfaces. The process, while involving several steps, offers a powerful approach to building modern web applications. The declarative style improves code readability and maintainability, making it a highly effective solution for structuring large and intricate React projects.