<?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>Forem: sheblmariam4</title>
    <description>The latest articles on Forem by sheblmariam4 (@sheblmariam4).</description>
    <link>https://forem.com/sheblmariam4</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%2F943844%2Fe3f42865-13eb-4e67-a918-c56f9270dbc2.png</url>
      <title>Forem: sheblmariam4</title>
      <link>https://forem.com/sheblmariam4</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sheblmariam4"/>
    <language>en</language>
    <item>
      <title>React Router v-6</title>
      <dc:creator>sheblmariam4</dc:creator>
      <pubDate>Wed, 08 Feb 2023 19:59:54 +0000</pubDate>
      <link>https://forem.com/sheblmariam4/react-router-v-6-12b7</link>
      <guid>https://forem.com/sheblmariam4/react-router-v-6-12b7</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;How to Install React Router&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To install React Router run &lt;code&gt;npm install react-router-dom@6&lt;/code&gt; in your react project terminal&lt;br&gt;
If you are using yarn then use this command: &lt;code&gt;yarn add react-router-dom@6&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Set Up&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To do this, open the   &lt;code&gt;index.js&lt;/code&gt;  file in the  &lt;code&gt;src&lt;/code&gt;  folder and import   &lt;code&gt;BrowserRouter&lt;/code&gt;  from    &lt;code&gt;react-router-dom&lt;/code&gt;  and then wrap the root component&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 App from './App';

ReactDOM.render(
  &amp;lt;BrowserRouter&amp;gt;
    &amp;lt;App /&amp;gt;
  &amp;lt;/BrowserRouter&amp;gt;,
  document.getElementById('root')
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;How to Route to Other Components&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 1 : Create multiple components&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We'll create the following  &lt;code&gt;Home&lt;/code&gt; , &lt;code&gt;About&lt;/code&gt; ,and  &lt;code&gt;Contact&lt;/code&gt; components like this:&lt;/p&gt;

&lt;p&gt;Home component&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Home=()=&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;This is the home page&amp;lt;/h1&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default Home;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;About component&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const About=()=&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;This is the About page&amp;lt;/h1&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default About;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Contact  component&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Contact=()=&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;This is the Contact page&amp;lt;/h1&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default Contact;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2 : Define routes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;App&lt;/code&gt;component acts as the root component that our React code initially renders, so we will create all our routes there.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Routes, Route } from "react-router-dom"
import Home from "./Home"
import About from "./About"
import Contact from "./Contact"

function App() {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;Routes&amp;gt;
        &amp;lt;Route path="/" element={ &amp;lt;Home/&amp;gt; } /&amp;gt;
        &amp;lt;Route path="/about" element={ &amp;lt;About/&amp;gt; } /&amp;gt;
        &amp;lt;Route path="/contact" element={ &amp;lt;Contact/&amp;gt; } /&amp;gt;
      &amp;lt;/Routes&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}

export default App
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Routes&lt;/code&gt;acts as a container for all the individual routes that  created in the app.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Route&lt;/code&gt;is used to create a single route, It takes in two attributes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;path&lt;/code&gt; which specifies the URL path of the desired component.   you'll notice that the first pathname is a backslash (/)which will get rendered first whenever the app loads for the first time. This implies that the Home component will be the first component to get rendered.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;element&lt;/code&gt; which specifies the component the route should render.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Step 3 : navigate to routes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We will use &lt;code&gt;Link&lt;/code&gt;to navigate between pages/components&lt;br&gt;
The &lt;code&gt;Link&lt;/code&gt; component is like element &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; in HTML. Its to attribute specifies which path the link takes you to.&lt;/p&gt;

&lt;p&gt;we will create a new component called &lt;code&gt;Navbar.js&lt;/code&gt; like this and navigate between components&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Link } from "react-router-dom";

const Navbar=()=&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;This is the home page&amp;lt;/h1&amp;gt;
      &amp;lt;Link to="/about"&amp;gt;Click to view our about page&amp;lt;/Link&amp;gt;
      &amp;lt;Link to="/contact"&amp;gt;Click to view our contact page&amp;lt;/Link&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default Navbar;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the &lt;code&gt;App&lt;/code&gt;component will be like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Routes, Route } from "react-router-dom"
import Home from "./Home"
import About from "./About"
import Contact from "./Contact"
import Navbar from "./Navbar"
function App() {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;Navbar/&amp;gt;
      &amp;lt;Routes&amp;gt;
        &amp;lt;Route path="/" element={ &amp;lt;Home/&amp;gt; } /&amp;gt;
        &amp;lt;Route path="/about" element={ &amp;lt;About/&amp;gt; } /&amp;gt;
        &amp;lt;Route path="/contact" element={ &amp;lt;Contact/&amp;gt; } /&amp;gt;
      &amp;lt;/Routes&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}

export default App
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>productivity</category>
      <category>ai</category>
      <category>programming</category>
      <category>career</category>
    </item>
  </channel>
</rss>
