{"id":4503,"date":"2018-05-25T09:45:30","date_gmt":"2018-05-25T04:15:30","guid":{"rendered":"https:\/\/www.innovationm.com\/blog\/?p=4503"},"modified":"2018-05-25T09:45:30","modified_gmt":"2018-05-25T04:15:30","slug":"getting-started-with-react-router","status":"publish","type":"post","link":"https:\/\/www.innovationm.com\/blog\/getting-started-with-react-router\/","title":{"rendered":"Getting Started with React Router"},"content":{"rendered":"<p>Routing concept is very importance in almost every web application&#8217;s architecture, which could not be left out in the React. Power of routing enables us to make a full fleshed\u00a0single page applications with React. We\u00a0can make use of\u00a0React-Router for Routing in React.<\/p>\n<h4>Setup and Installation<\/h4>\n<p class=\"selectionShareable\">We need:<\/p>\n<ul>\n<li>Node.js\u00a0 and\u00a0npm.<\/li>\n<li>create\u00a0 a new project.<\/li>\n<\/ul>\n<p class=\"selectionShareable\">React Router is mainly composed of these packages:\u00a0<code>react-router<\/code>,\u00a0<code>react-router-dom<\/code>, and\u00a0<code>react-router-native<\/code>.<\/p>\n<p>Create a new project with\u00a0<em>create_react_app<\/em>\u00a0and navigate to the directory created as shown below:<\/p>\n<pre class=\"lang:default decode:true\">create-react-app innovationm\r\ncd innovationm<\/pre>\n<p>Install\u00a0<code>react-router-dom<\/code><\/p>\n<pre class=\"lang:default decode:true\">npm install --save react-router-dom<\/pre>\n<p>There are two types of Router that you can use in your React\u00a0 application. The\u00a0<code>BrowserRouter<\/code>\u00a0and\u00a0<code>HashRouter<\/code>. You have seen that many url was previously containing\u00a0 <code>#<\/code>\u00a0which is not being used now a days. So to acheive that, we use\u00a0<code>HashRouter<\/code>\u00a0 where the url before <code>#<\/code>indicates the server address and after hash the url is managed by react router . But now a days we see url without <code>#\u00a0<\/code>for that we can use\u00a0\u00a0<code>BrowserRouter<\/code>\u00a0 .<\/p>\n<p><strong>Index.js<\/strong><\/p>\n<pre class=\"lang:default decode:true\">import React from 'react';\r\nimport ReactDOM from 'react-dom';\r\nimport { BrowserRouter as Router, Route, Link } from \"react-router-dom\";\r\nimport '.\/index.css';\r\nimport App from '.\/App';\r\nimport registerServiceWorker from '.\/registerServiceWorker';\r\n\r\nReactDOM.render(\r\n  &lt;Router&gt;\r\n    &lt;App \/&gt;\r\n  &lt;\/Router&gt;, document.getElementById('root'));\r\nregisterServiceWorker();<\/pre>\n<p>In the code above, I imported the\u00a0<code>BrowserRouter<\/code>,\u00a0<code>Route<\/code>\u00a0from\u00a0<code>react-router-dom<\/code>. And I wrapped the\u00a0<code>&lt;App\/&gt;<\/code>\u00a0component with\u00a0<code>Router<\/code>\u00a0which is the alias of\u00a0<code>BrowserRouter<\/code>. The Router component is the first step to routing successfully. The Router component can only have one child element or component. So, in this way we wil[l define the routes.<\/p>\n<p><strong>app.js<\/strong><\/p>\n<pre class=\"lang:default decode:true \">import React, { Component } from 'react';\r\nimport { Route, Link } from 'react-router-dom';\r\nimport '.\/App.css';\r\n\r\n\r\nconst Home = () =&gt; (\r\n  &lt;div&gt;\r\n    &lt;h2&gt; Home &lt;\/h2&gt;\r\n  &lt;\/div&gt;\r\n);\r\n\r\nconst Language = () =&gt; (\r\n  &lt;div&gt;\r\n     &lt;ul&gt;\r\n      &lt;li&gt;Java&lt;\/li&gt;\r\n      &lt;li&gt;Python&lt;\/li&gt;\r\n      &lt;li&gt;R-Language&lt;\/li&gt;\r\n    &lt;\/ul&gt;\r\n  &lt;\/div&gt;\r\n);\r\n\r\nconst City = () =&gt; (\r\n  &lt;div&gt;\r\n    &lt;ul&gt;\r\n      &lt;li&gt;Delhi\/li&gt;\r\n      &lt;li&gt;Mumbai&lt;\/li&gt;\r\n      &lt;li&gt;Patna&lt;\/li&gt;\r\n    &lt;\/ul&gt;\r\n  &lt;\/div&gt;\r\n);\r\n\r\nclass App extends Component {\r\n  render() {\r\n    return (\r\n      &lt;div&gt;\r\n        &lt;ul&gt;\r\n          &lt;li&gt;&lt;Link to=\"\/\"&gt;Home&lt;\/Link&gt;&lt;\/li&gt;\r\n          &lt;li&gt;&lt;Link to=\"\/languages\"&gt;Language&lt;\/Link&gt;&lt;\/li&gt;\r\n          &lt;li&gt;&lt;Link to=\"\/cities\"&gt;Cities&lt;\/Link&gt;&lt;\/li&gt;\r\n        &lt;\/ul&gt;\r\n\r\n        &lt;Route path=\"\/\" component={Home}\/&gt;\r\n        &lt;Route path=\"\/airports\" component={Language}\/&gt;\r\n        &lt;Route path=\"\/cities\" component={City}\/&gt;\r\n      &lt;\/div&gt;\r\n    );\r\n  }\r\n}\r\n\r\nexport default App;<\/pre>\n<p>In the code above, we have links that should direct the user to\u00a0<code>\/<\/code>,\u00a0<code>\/languages<\/code>, and \/<code>cities<\/code>\u00a0using the\u00a0<code>&lt;Link&gt;<\/code>\u00a0component.<\/p>\n<p><code>Home<\/code><\/p>\n<pre class=\"lang:default decode:true\">&lt;Route path=\"\/\" exact component={Home}\/&gt;\r\n&lt;Route path=\"\/airports\" component={Language}\/&gt;\r\n&lt;Route path=\"\/cities\" component={City}\/&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>Component should be rendered only on the\u00a0<code>\/<\/code>, root route. However, it is rendered on all the routes. The\u00a0<code>\/<\/code>\u00a0matches\u00a0<code>\/airports<\/code>and\u00a0<code>\/cities<\/code>\u00a0routes, therefore rendering its component in these two other routes. The solution to this is to simply add the\u00a0<code>exact<\/code>\u00a0prop to the\u00a0<code>\/<\/code>\u00a0route.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Routing concept is very importance in almost every web application&#8217;s architecture, which could not be left out in the React. Power of routing enables us to make a full fleshed\u00a0single page applications with React. We\u00a0can make use of\u00a0React-Router for Routing in React. Setup and Installation We need: Node.js\u00a0 and\u00a0npm. create\u00a0 a new project. React Router [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4560,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[257,258],"tags":[247,259,260],"class_list":["post-4503","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react","category-web-technology","tag-react","tag-web","tag-web-technologies"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Getting Started with React Router - InnovationM - Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.innovationm.com\/blog\/getting-started-with-react-router\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Getting Started with React Router - InnovationM - Blog\" \/>\n<meta property=\"og:description\" content=\"Routing concept is very importance in almost every web application&#8217;s architecture, which could not be left out in the React. Power of routing enables us to make a full fleshed\u00a0single page applications with React. We\u00a0can make use of\u00a0React-Router for Routing in React. Setup and Installation We need: Node.js\u00a0 and\u00a0npm. create\u00a0 a new project. React Router [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.innovationm.com\/blog\/getting-started-with-react-router\/\" \/>\n<meta property=\"og:site_name\" content=\"InnovationM - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2018-05-25T04:15:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/05\/reactRouter.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"InnovationM Admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"InnovationM Admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/getting-started-with-react-router\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/getting-started-with-react-router\\\/\"},\"author\":{\"name\":\"InnovationM Admin\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"headline\":\"Getting Started with React Router\",\"datePublished\":\"2018-05-25T04:15:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/getting-started-with-react-router\\\/\"},\"wordCount\":276,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/getting-started-with-react-router\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/05\\\/reactRouter.png\",\"keywords\":[\"react\",\"Web\",\"Web Technologies\"],\"articleSection\":[\"React\",\"Web Technology\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/getting-started-with-react-router\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/getting-started-with-react-router\\\/\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/getting-started-with-react-router\\\/\",\"name\":\"Getting Started with React Router - InnovationM - Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/getting-started-with-react-router\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/getting-started-with-react-router\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/05\\\/reactRouter.png\",\"datePublished\":\"2018-05-25T04:15:30+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/getting-started-with-react-router\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/getting-started-with-react-router\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/getting-started-with-react-router\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/05\\\/reactRouter.png\",\"contentUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/05\\\/reactRouter.png\",\"width\":1280,\"height\":720},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/getting-started-with-react-router\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Getting Started with React Router\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\",\"name\":\"InnovationM - Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\",\"name\":\"InnovationM Admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"caption\":\"InnovationM Admin\"},\"sameAs\":[\"http:\\\/\\\/www.innovationm.com\\\/\"],\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/author\\\/innovationmadmin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Getting Started with React Router - InnovationM - Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.innovationm.com\/blog\/getting-started-with-react-router\/","og_locale":"en_US","og_type":"article","og_title":"Getting Started with React Router - InnovationM - Blog","og_description":"Routing concept is very importance in almost every web application&#8217;s architecture, which could not be left out in the React. Power of routing enables us to make a full fleshed\u00a0single page applications with React. We\u00a0can make use of\u00a0React-Router for Routing in React. Setup and Installation We need: Node.js\u00a0 and\u00a0npm. create\u00a0 a new project. React Router [&hellip;]","og_url":"https:\/\/www.innovationm.com\/blog\/getting-started-with-react-router\/","og_site_name":"InnovationM - Blog","article_published_time":"2018-05-25T04:15:30+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/05\/reactRouter.png","type":"image\/png"}],"author":"InnovationM Admin","twitter_misc":{"Written by":"InnovationM Admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.innovationm.com\/blog\/getting-started-with-react-router\/#article","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/getting-started-with-react-router\/"},"author":{"name":"InnovationM Admin","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"headline":"Getting Started with React Router","datePublished":"2018-05-25T04:15:30+00:00","mainEntityOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/getting-started-with-react-router\/"},"wordCount":276,"commentCount":0,"image":{"@id":"https:\/\/www.innovationm.com\/blog\/getting-started-with-react-router\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/05\/reactRouter.png","keywords":["react","Web","Web Technologies"],"articleSection":["React","Web Technology"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.innovationm.com\/blog\/getting-started-with-react-router\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.innovationm.com\/blog\/getting-started-with-react-router\/","url":"https:\/\/www.innovationm.com\/blog\/getting-started-with-react-router\/","name":"Getting Started with React Router - InnovationM - Blog","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/getting-started-with-react-router\/#primaryimage"},"image":{"@id":"https:\/\/www.innovationm.com\/blog\/getting-started-with-react-router\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/05\/reactRouter.png","datePublished":"2018-05-25T04:15:30+00:00","author":{"@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"breadcrumb":{"@id":"https:\/\/www.innovationm.com\/blog\/getting-started-with-react-router\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.innovationm.com\/blog\/getting-started-with-react-router\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.innovationm.com\/blog\/getting-started-with-react-router\/#primaryimage","url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/05\/reactRouter.png","contentUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/05\/reactRouter.png","width":1280,"height":720},{"@type":"BreadcrumbList","@id":"https:\/\/www.innovationm.com\/blog\/getting-started-with-react-router\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.innovationm.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Getting Started with React Router"}]},{"@type":"WebSite","@id":"https:\/\/www.innovationm.com\/blog\/#website","url":"https:\/\/www.innovationm.com\/blog\/","name":"InnovationM - Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.innovationm.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed","name":"InnovationM Admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","caption":"InnovationM Admin"},"sameAs":["http:\/\/www.innovationm.com\/"],"url":"https:\/\/www.innovationm.com\/blog\/author\/innovationmadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/4503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/comments?post=4503"}],"version-history":[{"count":0,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/4503\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media\/4560"}],"wp:attachment":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media?parent=4503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/categories?post=4503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/tags?post=4503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}