React Router
Introduction
What is react router?
React Router is a standard library for routing in React. It enables the navigation among views of various components in a React Application, allows changing the browser URL and keeps the UI in sync with the URL.
Is react router is static or dynamic?
- Before the react router v4 it is static after v4 it is Dynamic.
In single page apps, there is only single html page.we are reusing the same html page to render the different components based on the navigation.
But in multipage apps, you will get an entirely new page from the server when you navigate.
When we say dynamic routing, we mean routing that takes place as your app is rendering, not in a configuration or convention outside of a running app.
How to install the react router?
we are using the create-react-app to create the app.
npx create-react-app routing
cd routing
To install the react router you need to download the react-router-dom package by running the following commands.
npm install react-router-dom
npm start //to run dev server
To add React Router components in your application, open your project directory in the editor you use and go to app.js file. Now, add the below given code in app.js:-
Note: BrowserRouter is aliased as Router.
Get Shilpa Rai’s stories in your inbox
Join Medium for free to get updates from this writer.
To use React Router, let us first create few components in the react application. In your project directory, create a folder named component inside the src folder and now add 3 files named home.js, about.js and contact.js to the component folder.
Let us add some code to our 3 components:
Now, let us include React Router components to the application:
- BrowserRouter: Add BrowserRouter aliased as Router to your app.js file in order to wrap all the other components. BrowserRouter is a parent component and can have only single child.
Link: Let us now create links to our components. Link component uses the to prop to describe the location where the links should navigate to.
Now, run your application on the local host and click on the links you created. You will notice the url changing according the value in to props of the Link component.
Route: Route component will now help us to establish the link between component’s UI and the URL. To include routes to the application, add the code give below to your app.js.
Components are linked now and clicking on any link will render the component associated with it.
Let us now try to understand the props associated with the Route component.
- 1.exact: It is used to match the exact value with the URL. For Eg., exact path=’/about’ will only render the component if it exactly matches the path but if we remove exact from the syntax, then UI will still be rendered even if the structure is like /about/10.
- 2. path: Path specifies a pathname we assign to our component.
- 3. component: It refers to the component which will render on matching the path.
Note: By default, routes are inclusive which means more than one Route component can match the URL path and render at the same time. If we want to render a single component, we need to use switch.
- Switch: To render a single component, wrap all the routes inside the Switch Component.
Switch groups together several routes, iterates over them and finds the first one that matches the path. Thereby, the corresponding component to the path is rendered.
After adding all the components here is our complete source code:
Now, we you can click on the links and navigate to different components. React Router keeps your application UI in sync with the URL.






