Why switch keyword used in React Router v4 ?
Last Updated :
18 Oct, 2021
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
What is StaticHandler in React Router
React Router is a popular library in the React ecosystem that enables routing in single-page applications (SPAs). It allows developers to define navigation and rendering rules based on the URL of the application. One of the essential components of React Router is the StaticHandler, which plays a cru
5 min read
Why we need Redux in React ?
Redux is a library used in JavaScript applications for managing application states. It is particularly used and more popular in terms of building single-page applications using frameworks like React. Redux can also be used with other frameworks or libraries as well. It serves as a centralized store
7 min read
What are the React Router hooks in React v5?
React Router hooks perform client-side single-page routing that provides a functional and streamlined approach to managing navigation in React applications. It provides a way to directly access the state of the router from functional components, this simplifies tasks like retrieving information abou
5 min read
What is RouterProvider in React Router ?
The RouterProvider in React Router is similar to the traffic controller of our React apps. It helps us monitor how users move between pages and objects in a single-page application (SPA). Essentially, itâs the backbone of React Router, handling all the routing magic behind the scenes. Table of Conte
5 min read
How to setup React Router v6 ?
React Router is an amazing library that was created by Remix. The purpose of React Router is to enable Client-Side routing in the React application, that is, by using React Router we can create different routes in our React app and can easily navigate to different pages without even reloading the pa
6 min read
How to use the useHistory hook in React Router?
React Router is a standard library system built on top of React and used to create routing in the React application using the React Router Package. It enables the navigation between views of various components in a React Application. React Router is useful for Single Page Applications (SPAs). To use
4 min read
Why we use synthetic events in React JS ?
In React JS, there are events by which users interact the with an application UI. React listens to events at the document level, after receiving events from the browser, React wraps these events with a wrapper that has a similar interface as the local browser event, which helps us to use methods lik
2 min read
Migrate to React Router v6
React Router v6 brings significant changes and improvements over its predecessor, offering a more intuitive API and better performance. Migrating to React Router v6 might seem daunting, but with proper guidance, the transition can be smooth and beneficial for your React applications. Enhancements in
3 min read
Explain StaticRouter in React Router
React Router is a powerful library for handling routing in React applications, allowing developers to create dynamic single-page applications (SPAs) with ease. While the BrowserRouter is commonly used for client-side routing in web applications, there are scenarios where server-side rendering (SSR)
4 min read
StaticRouterProvider in React Router
React Router, a prominent library for handling routing in React applications, includes a vital component known as StaticRouterProvider. This article aims to shed light on the functionality and importance of StaticRouterProvider, particularly in scenarios involving server-side rendering (SSR) with Re
3 min read