How to implement React Routing

Search for a command to run...

No comments yet. Be the first to comment.
Twitter is one of the most popular social media platforms in the world, with over 330 million active users as of 2021. If you are interested in building a Twitter-like application, this tutorial will guide you through the process of building a FullSt...

Hey everyone, In this article, we will build our own Tic Tac Toe game using pure Javascript. https://youtu.be/lKe57l8sttw Code <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE...

Hi everyone, In this blog, we will learn how we can create node.js functions locally and deploy them to AWS Lambda using serverless. So, let's get started. Step 1. Create an AWS Account ☁️ In order to deploy functions to AWS Lambda, you should have a...

Hey there, In this article, we are going to learn how you can upload any file to firebase storage. Uploading files to firebase storage is really easy. I would I recommend you to code along with me for better understanding. So, let's get started. Step...

Hey there, Welcome back. In this article, I would be showing you how I made a collaborative IDE with a video call. The main application of this application is that you can create a room and people can join the room. You have the ability to write mark...

In this article, I will be explaining how to implement routing in react app in very simple steps.
npm install react-router-dom
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import {BrowserRouter} from 'react-router-dom'
ReactDOM.render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>,
document.getElementById('root')
);
serviceWorker.unregister();
In the above code, we have wrapped our <App/> component with <BrowserRouter /> which enables us to use Html 5 history API.
import React from 'react';
import { Switch, Route } from 'react-router-dom'
import './App.css';
import Conatct from './pages/contact'
import Blog from './pages/blog'
import Index from './pages/index'
function App() {
return (
<Switch>
<Route path='/contact' component={ <Contact />}/>
<Route path='/blogs' component={<Blog />}/>
<Route exact path='/' component={<Index />}/>
</Switch>
);
}
export default App;
In app.js we need to implement switch cases with various routes as shown in the above code.
Each Route component needs two mandatory props and that is the path and the component to render.
Congratulations, you have now implemented routing in your react app.