<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mehadi Hasan</title>
    <description>The latest articles on DEV Community by Mehadi Hasan (@mehadi).</description>
    <link>https://dev.to/mehadi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F454289%2Fbd18fcf3-3308-48e5-9ff5-1c4e4d7913ef.jpeg</url>
      <title>DEV Community: Mehadi Hasan</title>
      <link>https://dev.to/mehadi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mehadi"/>
    <language>en</language>
    <item>
      <title>How to install React Router and use it on React</title>
      <dc:creator>Mehadi Hasan</dc:creator>
      <pubDate>Wed, 14 Oct 2020 14:38:24 +0000</pubDate>
      <link>https://dev.to/mehadi/how-to-install-react-router-and-use-it-on-react-4on1</link>
      <guid>https://dev.to/mehadi/how-to-install-react-router-and-use-it-on-react-4on1</guid>
      <description>&lt;p&gt;Today you will learn about the React Router and at we will implement React Router on our project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table Of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What is React Router?&lt;/li&gt;
&lt;li&gt;Creating New Project&lt;/li&gt;
&lt;li&gt;How to install the React Router?&lt;/li&gt;
&lt;li&gt;Routing&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is React Router
&lt;/h2&gt;

&lt;p&gt;React Router is a routing library which is used to create routing in React app. After the v4 the React Router works dynamically, previously it was static.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating New Project
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app react-route
cd react-route
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to install the React Router?
&lt;/h2&gt;

&lt;p&gt;Run the below command to download react-router-dom package.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install react-router-dom //download and install the package
npm start  //start development server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Routing
&lt;/h2&gt;

&lt;p&gt;After starting the server you can view the app from &lt;strong&gt;&lt;a href="http://localhost:3000"&gt;http://localhost:3000&lt;/a&gt;.&lt;/strong&gt; Open the project in your favorite editor and navigate to &lt;strong&gt;public/index.html.&lt;/strong&gt; Here is the updated code of index.html&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="utf-8" /&amp;gt;
    &amp;lt;link rel="icon" href="%PUBLIC_URL%/favicon.ico" /&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1" /&amp;gt;
    &amp;lt;meta name="theme-color" content="#000000" /&amp;gt;
    &amp;lt;meta
      name="description"
      content="Web site created using create-react-app"
    /&amp;gt;
    &amp;lt;link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" /&amp;gt;
    &amp;lt;link rel="manifest" href="%PUBLIC_URL%/manifest.json" /&amp;gt;
    &amp;lt;link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous"&amp;gt;
    &amp;lt;title&amp;gt;React Router Example&amp;lt;/title&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;noscript&amp;gt;You need to enable JavaScript to run this app.&amp;lt;/noscript&amp;gt;
    &amp;lt;div id="root"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;/body&amp;gt;
  &amp;lt;script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have to create component for our app. Create a folder under src named components. Create three file Navbar.js, About.js, Contact.js&lt;/p&gt;

&lt;p&gt;Edit Navbar.js and paste this code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react'
import {Link} from 'react-router-dom';
class About extends React.Component {
    render() {
        return &amp;lt;div
            className="d-flex flex-column flex-md-row align-items-center p-3 px-md-4 mb-3 bg-white border-bottom shadow-sm"&amp;gt;
            &amp;lt;h5 className="my-0 mr-md-auto font-weight-normal"&amp;gt;&amp;lt;Link className="p-2 text-dark" to="/"&amp;gt;React Router Example&amp;lt;/Link&amp;gt;&amp;lt;/h5&amp;gt;
            &amp;lt;nav className="my-2 my-md-0 mr-md-3"&amp;gt;
                &amp;lt;Link className="p-2 text-dark" to="/about"&amp;gt;About&amp;lt;/Link&amp;gt;
                &amp;lt;Link className="p-2 text-dark" to="/contact"&amp;gt;Contact&amp;lt;/Link&amp;gt;
            &amp;lt;/nav&amp;gt;
        &amp;lt;/div&amp;gt;
    }
}
export default About
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Edit About.js and paste this code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react'
class About extends React.Component {
    render() {
        return &amp;lt;div className="card"&amp;gt;
            &amp;lt;div className="card-header"&amp;gt;
                About
            &amp;lt;/div&amp;gt;
            &amp;lt;div className="card-body"&amp;gt;
               &amp;lt;p&amp;gt;
                   Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
               &amp;lt;/p&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
    }
}
export default About
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Edit Contact.js and paste this code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react'
class Contact extends React.Component {
    render() {
        return &amp;lt;div className="card"&amp;gt;
            &amp;lt;div className="card-header"&amp;gt;
                Contact
            &amp;lt;/div&amp;gt;
            &amp;lt;div className="card-body"&amp;gt;
                &amp;lt;form&amp;gt;
                    &amp;lt;div className="form-group"&amp;gt;
                        &amp;lt;label &amp;gt;Name&amp;lt;/label&amp;gt;
                        &amp;lt;input type="text" className="form-control"
                               placeholder="Enter name"/&amp;gt;
                    &amp;lt;/div&amp;gt;

                    &amp;lt;div className="form-group"&amp;gt;
                        &amp;lt;label &amp;gt;Message&amp;lt;/label&amp;gt;
                        &amp;lt;textarea className="form-control" id="message" name="message"
                                  placeholder="Please enter your message here..." rows="5"/&amp;gt;
                    &amp;lt;/div&amp;gt;


                    &amp;lt;button type="submit" className="btn btn-primary"&amp;gt;Submit&amp;lt;/button&amp;gt;
                &amp;lt;/form&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
    }
}
export default Contact
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now open App.js and paste these code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react'
import {Link} from 'react-router-dom';
class App extends React.Component {
  render() {
    return (
        &amp;lt;div className="card"&amp;gt;
            &amp;lt;div className="card-header"&amp;gt;
                Homepage
            &amp;lt;/div&amp;gt;
            &amp;lt;div className="card-body"&amp;gt;
                &amp;lt;h1&amp;gt;Weclome to Homepage! Content shoing from App.js&amp;lt;/h1&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
    )
  }
}
export default App
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we at at last part of our tutorial open index.js and update the code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react'
import ReactDOM from 'react-dom'
import './index.css'
import { Route, Link,Switch, BrowserRouter as Router } from 'react-router-dom'
import App from './App';
import NavBar from './components/Navbar'
import Contact from './components/Contact';
import About from './components/About';
const routing = (
    &amp;lt;Router&amp;gt;
        &amp;lt;div&amp;gt;

            &amp;lt;NavBar/&amp;gt;

            &amp;lt;section className="container"&amp;gt;
                &amp;lt;Switch&amp;gt;
                    &amp;lt;Route exact path="/" component={App} /&amp;gt;
                    &amp;lt;Route exact path="/about" component={About} /&amp;gt;
                    &amp;lt;Route exact path="/contact" component={Contact} /&amp;gt;
                &amp;lt;/Switch&amp;gt;
            &amp;lt;/section&amp;gt;

        &amp;lt;/div&amp;gt;
    &amp;lt;/Router&amp;gt;
)
ReactDOM.render(routing, document.getElementById('root'))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>react</category>
      <category>reactrouter</category>
      <category>installreact</category>
    </item>
  </channel>
</rss>
