<?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: janjibDEV</title>
    <description>The latest articles on DEV Community by janjibDEV (@janjibdev).</description>
    <link>https://dev.to/janjibdev</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%2F396377%2Fbbe66e2d-5b08-4427-b9a8-35908589792a.jpeg</url>
      <title>DEV Community: janjibDEV</title>
      <link>https://dev.to/janjibdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/janjibdev"/>
    <language>en</language>
    <item>
      <title>Guide To : React Routing !</title>
      <dc:creator>janjibDEV</dc:creator>
      <pubDate>Thu, 19 Aug 2021 15:35:58 +0000</pubDate>
      <link>https://dev.to/janjibdev/guide-to-react-routing-1di7</link>
      <guid>https://dev.to/janjibdev/guide-to-react-routing-1di7</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Routing in react application is really useful especially when it comes to building an app with multiple pages.It is easy to implement in your project and make your project more organize !&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;So first of all, you must install the &lt;code&gt;react-router-dom&lt;/code&gt; package by running &lt;code&gt;npm install react-router-dom&lt;/code&gt; in your integrated terminal.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;In simple words, react routing usually is used when you want to implement multiple pages in your app or selectively display some UI.&lt;/p&gt;

&lt;p&gt;To begin with, import some important component from react-router-dom library on top of your &lt;code&gt;App.js&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {BrowserRouter as Router,Switch,Route} from 'react-router-dom'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you see there are 3 important thing to begin with :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Router&lt;/li&gt;
&lt;li&gt;Switch&lt;/li&gt;
&lt;li&gt;Route&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Router
&lt;/h3&gt;

&lt;p&gt;Wrap all the tag in your &lt;code&gt;App.js&lt;/code&gt; with &lt;code&gt;&amp;lt;Router&amp;gt;&amp;lt;/Router&amp;gt;&lt;/code&gt; to allow function like &lt;code&gt;useHistory()&lt;/code&gt; to be used in your components.Otherwise, an error will be throw that tell you &lt;code&gt;useHistory()&lt;/code&gt; cannot be used outside the router.&lt;/p&gt;

&lt;h3&gt;
  
  
  Switch
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Switch&lt;/code&gt; tag must wrap all the &lt;code&gt;Route&lt;/code&gt; tag, to allow your route to functioning. Without &lt;code&gt;switch&lt;/code&gt; tag, all of your route will be display.&lt;/p&gt;

&lt;h3&gt;
  
  
  Route
&lt;/h3&gt;

&lt;p&gt;Route tag is responsible for providing a path for your component to be rendered.&lt;/p&gt;

&lt;p&gt;Here is the syntax for route :&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;Route path='/{put your path here !}'  exact component={put your component here !}&amp;gt;&amp;lt;/Route&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the example of how &lt;code&gt;Router&lt;/code&gt;, &lt;code&gt;Switch&lt;/code&gt;, &lt;code&gt;Route&lt;/code&gt; are placed on your &lt;code&gt;App.js&lt;/code&gt; :&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;div className="App"&amp;gt;

&amp;lt;Header/&amp;gt;

&amp;lt;Router&amp;gt;

&amp;lt;Switch&amp;gt;

&amp;lt;Route path='/'  exact component={TouristList}&amp;gt;&amp;lt;/Route&amp;gt; 

&amp;lt;Route path='/detail/:id' exact component={TouristDetail}&amp;gt;&amp;lt;/Route&amp;gt;

&amp;lt;/Switch&amp;gt;

&amp;lt;/Router&amp;gt;

&amp;lt;/div&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Note :
&lt;/h4&gt;

&lt;p&gt;You may be wondering why I dont put my &lt;code&gt;&amp;lt;Header/&amp;gt;&lt;/code&gt; inside the &lt;code&gt;Router&lt;/code&gt; tag ? That is because in my &lt;code&gt;Header&lt;/code&gt; component, there is navigation bar for my app and I want my navigation bar will always be displayed no matter on what page the user is.&lt;/p&gt;

&lt;p&gt;Okay, now I have setup the routing, how am I going to another page ? Route ?&lt;/p&gt;

&lt;p&gt;Well, this is when we will used a function from &lt;code&gt;react-router-dom&lt;/code&gt; library to switch into another route which is &lt;code&gt;useHistory()&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Note :
&lt;/h4&gt;

&lt;p&gt;There is also called component called &lt;code&gt;&amp;lt;Link&amp;gt;&lt;/code&gt; that has similar function to useHistory() ! However, I dont like to use it because it basically like wrapping an &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tag, and if you realize it will cause some problem in your css. (You may give it a try !)&lt;/p&gt;

&lt;h3&gt;
  
  
  useHistory()
&lt;/h3&gt;

&lt;p&gt;First we must import &lt;code&gt;useHistory()&lt;/code&gt; function from &lt;code&gt;react-router-dom&lt;/code&gt; library on top of your js file.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Next create an instance of useHistory inside of your 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 App = () =&amp;gt; {
  let history = useHistory()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then,we can use one of useHistory method which is push in your JSX element like button.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const App = () =&amp;gt; {
  let history = useHistory()

 return(
        &amp;lt;&amp;gt;
           &amp;lt;button 
            onClick={()=&amp;gt;history.push('/nextpage')&amp;gt;
            Next Page
            &amp;lt;/button&amp;gt;
        &amp;lt;/&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;.push()&lt;/code&gt; is one of the array method that we used to insert an element at the end of an array.In terms of routing, our app will rendered the last component in an array.So by pushing a new path to the end of the array, we literally tell our app to render the a new component.&lt;/p&gt;

&lt;h3&gt;
  
  
  useParams()
&lt;/h3&gt;

&lt;p&gt;The last function that I want to explain is useParam(). Well, this function allow us to pass information through path.&lt;/p&gt;

&lt;p&gt;So we must declare that we want to pass a parameter (information) through path by adding &lt;code&gt;/:{parameter name}&lt;/code&gt; to the end of your path.&lt;/p&gt;

&lt;p&gt;Example :&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;Route path='/detail/:id' exact component={TouristDetail}&amp;gt;&amp;lt;/Route&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have added a parameter called &lt;code&gt;id&lt;/code&gt;. So, for me to pass information using &lt;code&gt;useHistory&lt;/code&gt; just 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;onClick={()=&amp;gt; history.push(`/detail/${id}`)}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that I insert 12 as the &lt;code&gt;id&lt;/code&gt; .This will be really handy when you want to perform data fetching in the next page like checkout page in e-commerce website. I also use backtick&lt;br&gt;
&lt;br&gt;
 ```&lt;code&gt;instead of normal quote&lt;/code&gt;''` so that I can easily pass my variable or state.&lt;/p&gt;

&lt;p&gt;And in order to access the parameter that we pass, we can use &lt;code&gt;useParams()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const TouristDetails = () =&amp;gt; {

// Note that I destructure the object that I received from useParams()
const {id} = useParams()

return &amp;lt;h1&amp;gt;{id}&amp;lt;h1&amp;gt;
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I think thats all from me. Hope everything that I wrote can be implemented in your future project.&lt;/p&gt;

</description>
      <category>react</category>
      <category>programming</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Problem with react-router app and Github Pages (Solved !)</title>
      <dc:creator>janjibDEV</dc:creator>
      <pubDate>Mon, 02 Aug 2021 10:36:29 +0000</pubDate>
      <link>https://dev.to/janjibdev/problem-with-react-router-app-and-github-pages-lij</link>
      <guid>https://dev.to/janjibdev/problem-with-react-router-app-and-github-pages-lij</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Everyone must have using the GitHub pages to deploy their React project to the internet. And of course, building a react app with  the react-router library is a must when you are building a multi-page app. However, you will notice some problems when deploying the react-router app to the GitHub pages.&lt;/p&gt;

&lt;p&gt;This view will be displayed when we deploy react router app to GitHub pages :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb1gvx6jdu8hwb3tbvpwl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb1gvx6jdu8hwb3tbvpwl.png" alt="React app does not show up when deployed with Github pages"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The solution is to change our path of the route in our app:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import './App.css';
import {BrowserRouter as Router,Switch,Route} from 'react-router-dom'
import Homepage from './components/Homepage';
import SecondPage from './components/SecondPage';

function App() {
  return (
    &amp;lt;div className="App"&amp;gt;
     &amp;lt;Router&amp;gt;
       &amp;lt;Switch&amp;gt;
         &amp;lt;Route path='/' exact component={Homepage}/&amp;gt;
         &amp;lt;Route path='/secondpage' exact component={SecondPage}/&amp;gt;
       &amp;lt;/Switch&amp;gt;
     &amp;lt;/Router&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default App;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Notice the route tag , now we gonna change the path.&lt;/p&gt;

&lt;p&gt;the formula is by putting your app name (according to your repository name) at the beginning of the path (after / ) for the main page and other pages, just add the additional page behind the home path.&lt;/p&gt;

&lt;h3&gt;
  
  
  Formula : '/{your-app-name}/{route to another path}'
&lt;/h3&gt;

&lt;h6&gt;
  
  
  Like this :
&lt;/h6&gt;

&lt;p&gt;let say my app name is problem-showcase &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import './App.css';
import {BrowserRouter as Router,Switch,Route} from 'react-router-dom'
import Homepage from './components/Homepage';
import SecondPage from './components/SecondPage';

function App() {
  return (
    &amp;lt;div className="App"&amp;gt;
     &amp;lt;Router&amp;gt;
       &amp;lt;Switch&amp;gt;
         &amp;lt;Route path='/problem-showcase' exact component={Homepage}/&amp;gt;
         &amp;lt;Route path='/problem-showcase/secondpage' exact component={SecondPage}/&amp;gt;
       &amp;lt;/Switch&amp;gt;
     &amp;lt;/Router&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default App;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;See how I change the path of the route?&lt;/p&gt;

&lt;p&gt;The same thing goes when you use Link or useHistory in your app&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 {useHistory} from 'react-router-dom'

const Homepage = () =&amp;gt; {
    let history = useHistory()
    return (
        &amp;lt;div&amp;gt;
            &amp;lt;h1&amp;gt;Homepage&amp;lt;/h1&amp;gt;
            &amp;lt;button onClick={()=&amp;gt;history.push('/secondpage')}&amp;gt;Click here&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
    )
}

export default Homepage


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;note that I used useHistory from react-router-dom to navigate to the second page&lt;/p&gt;

&lt;p&gt;Since we have to change the path for the second page, we also need to change the path to the second page when using useHistory&lt;/p&gt;

&lt;p&gt;Like this:&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 {useHistory} from 'react-router-dom'

const Homepage = () =&amp;gt; {
    let history = useHistory()
    return (
        &amp;lt;div&amp;gt;
            &amp;lt;h1&amp;gt;Homepage&amp;lt;/h1&amp;gt;
            &amp;lt;button 
             onClick={()=&amp;gt;history.push('/problem-showcase/secondpage')}&amp;gt;
              Click here 
            &amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
    )
}

export default Homepage


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h6&gt;
  
  
  So, just note that we need to change the path to our route in the entire app.
&lt;/h6&gt;

&lt;p&gt;So now we can deploy our react-router app to Github pages. You can refer to this GitHub repository for the deployment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/gitname/react-gh-pages" rel="noopener noreferrer"&gt;Deploy react app to gh-pages&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just make sure your homepage URL (project repository name) in package.json is the same as the homepage path. &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

"homepage": "http://{your github name}.github.io/{your repository name}"

&amp;lt;Route path='/{your repository name}' exact component={Homepage}/&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h6&gt;
  
  
  Example:
&lt;/h6&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

"homepage": "http://janjib.github.io/problem-showcase"

&amp;lt;Route path='/problem-showcase' exact component={Homepage}/&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Done.&lt;/p&gt;

&lt;p&gt;This is the link to my Github repository:&lt;br&gt;
&lt;a href="https://github.com/janjib/problem-showcase" rel="noopener noreferrer"&gt;Click here !&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>github</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
