Why switch keyword used in React Router v4 ?
Last Updated :
23 Jul, 2025
ReactJS is a javascript library that is used to build single-page applications (SPA). React by default does not provide the routing features. We can implement routing in ReactJS projects using React Router. React Router is a library that enables navigation among views of various components in a React Application, allows changing the browser URL, and keeps the UI in sync with the URL.
In this article, we will see how routing works in React Router and how we can take advantage of the Switch component provided by React Router.
Creating React Application And Installing Module:
Step 1: Create a React application using the following command.
npx create-react-app SWITCH_DEMO_APP
Step 2: After creating your project folder, move to it by using the following command.
cd SWITCH_DEMO_APP
Step 3: After creating the React application, Install the React Router as a dependency using the following command.
npm install --save react-router-dom
Project Structure: The project structure after deleting some unrequired files is shown in the picture mentioned below.
Project StructureExample 1: Routing without Switch component - When we perform routing using React Router, whenever a page is rendered the URL path is being matched to each route. All the route paths that match the given URL path are rendered. The content of the App.js file is mentioned below. In this file, we have created four div, each containing a Link Component provided by React Router. We have also created five Routes i.e. home, profile, products, products/shoes, and a Route with a path equal to a * that is a wild card that matches with every URL path.
How does path matching work in React Router?
React router takes the relative URL and matches it with each path provided in the Route component. The route matching is done in a way that if a part of this relative URL is matched then that Route is rendered. For example, if the relative URL is /products/shoes then paths /, /products, and /products/shoes match the URL and all three routes are rendered but /profile does not match the URL.
Filename: App.js
JavaScript
import React, { Fragment } from "react";
import { BrowserRouter as Router, Link,
Route } from "react-router-dom";
const Home = () => {
return <h2>Home</h2>;
};
const Profile = () => {
return <h2>Profile</h2>;
};
const Shoes = () => {
return <h2>Shoes</h2>;
};
const Products = () => {
return <h2>Products</h2>;
};
const App = () => {
return (
<Fragment>
<Router>
<div>
<Link to="/">Home</Link>
</div>
<div>
<Link to="/products">Products</Link>
</div>
<div>
<Link to="/products/shoes">Shoes</Link>
</div>
<div>
<Link to="/profile">profile</Link>
</div>
<Route path="/">
<Home />
</Route>
<Route path="/profile">
<Profile />
</Route>
<Route path="/products">
<Products />
</Route>
<Route path="/products/shoes">
<Shoes />
</Route>
</Router>
</Fragment>
);
};
export default App;
Step to Run Application: Run the application using the following command from the root directory of the project.
npm start
Output: Image showing the Webpage when we click the 'shoes' link.

Example 2: Routing using Switch Component - When we wrap our routes inside the Switch component then it makes sure that only one route is rendered at a time. So in this case, the first route that matches the relative URL with the paths of each Route component and only renders the first path that matches the part of the relative URL as we have discussed above.
Syntax: Syntax to use the Switch component is mentioned below.
<Switch>
<Route exact path='/'>
<A />
</Route>
<Route path='/b'>
<B />
</Route>
<Route exact path='/c'>
<C />
</Route>
<Route path='/d'>
<D />
</Route>
</Switch>
If two routes match with each other then we can use the exact prop as shown below.
<Route exact path='/'>
<Home />
</Route>
By using this exact prop, we make sure that the home route is only rendered when the relative URL exactly matches /. Now if the relative URL is /profile then only the profile route is rendered doesn't matter if the profile Route is below / route in the code. The content of the App.js file when we use the Switch component is mentioned below.
Filename: App.js
JavaScript
import React, { Fragment } from 'react';
import { BrowserRouter as Router,
Link, Route, Switch } from 'react-router-dom';
const Home = () => {
return <h2>Home</h2>;
};
const Profile = () => {
return <h2>Profile</h2>;
};
const Shoes = () => {
return <h2>Shoes</h2>;
};
const Products = () => {
return <h2>Products</h2>;
};
const App = () => {
return (
<Fragment>
<Router>
<div>
<Link to='/'>Home</Link>
</div>
<div>
<Link to='/products'>Products</Link>
</div>
<div>
<Link to='/products/shoes'>Shoes</Link>
</div>
<div>
<Link to='/profile'>profile</Link>
</div>
<Switch>
<Route exact path='/'>
<Home />
</Route>
<Route path='/profile'>
<Profile />
</Route>
<Route exact path='/products'>
<Products />
</Route>
<Route path='/products/shoes'>
<Shoes />
</Route>
</Switch>
</Router>
</Fragment>
);
};
export default App;
Step to Run Application: Run the application using the following command from the root directory of the project.
npm start
Output: Image showing the Webpage when we click the 'shoes' link when using Switch component
Similar Reads
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
React Fundamentals
React IntroductionReactJS is a component-based JavaScript library used to build dynamic and interactive user interfaces. It simplifies the creation of single-page applications (SPAs) with a focus on performance and maintainability. "Hello, World!" Program in ReactJavaScriptimport React from 'react'; function App() {
6 min read
React Environment SetupTo run any React application, we need to first setup a ReactJS Development Environment. In this article, we will show you a step-by-step guide to installing and configuring a working React development environment.Pre-requisite:We must have Nodejs installed on our PC. So, the very first step will be
3 min read
React JS ReactDOMReactDOM is a core React package that provides DOM-specific methods to interact with and manipulate the Document Object Model (DOM), enabling efficient rendering and management of web page elements. ReactDOM is used for: Rendering Components: Displays React components in the DOM.DOM Manipulation: Al
2 min read
React JSXJSX stands for JavaScript XML, and it is a special syntax used in React to simplify building user interfaces. JSX allows you to write HTML-like code directly inside JavaScript, enabling you to create UI components more efficiently. Although JSX looks like regular HTML, itâs actually a syntax extensi
5 min read
ReactJS Rendering ElementsIn this article we will learn about rendering elements in ReactJS, updating the rendered elements and will also discuss about how efficiently the elements are rendered.What are React Elements?React elements are the smallest building blocks of a React application. They are different from DOM elements
3 min read
React ListsIn lists, React makes it easier to render multiple elements dynamically from arrays or objects, ensuring efficient and reusable code. Since nearly 85% of React projects involve displaying data collectionsâlike user profiles, product catalogs, or tasksâunderstanding how to work with lists.To render a
4 min read
React FormsForms are an essential part of any application used for collecting user data, processing payments, or handling authentication. React Forms are the components used to collect and manage the user inputs. These components include the input elements like text field, check box, date input, dropdowns etc.
5 min read
ReactJS KeysA key serves as a unique identifier in React, helping to track which items in a list have changed, been updated, or removed. It is particularly useful when dynamically creating components or when users modify the list. When rendering a list, you need to assign a unique key prop to each element in th
4 min read
Components in React
React Lifecycle In React, the lifecycle refers to the various stages a component goes through. These stages allow developers to run specific code at key moments, such as when the component is created, updated, or removed. By understanding the React lifecycle, you can better manage resources, side effects, and perfo
7 min read
React Hooks
Routing in React
Advanced React Concepts
React Projects