{"id":23414,"date":"2015-07-27T22:12:31","date_gmt":"2015-07-27T16:42:31","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=23414"},"modified":"2015-07-27T22:12:31","modified_gmt":"2015-07-27T16:42:31","slug":"routing-in-reactjs","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/routing-in-reactjs\/","title":{"rendered":"Routing  in ReactJS"},"content":{"rendered":"<h2 style=\"text-align: center\">React-Router<\/h2>\n<p>In this blog we will talk about <code>react-router<\/code>. Routing is a dire need in <strong>SPA<\/strong>(<em>Single page applications<\/em>), in which we load partial content without asking the server for a complete webpage. <code>React<\/code> is no different in terms of loading only what is required. Routing\u00a0programmatically presents specific content to users based on the <code>URL<\/code> that they are visiting. But, as we add more and more logic to an app, it grows very complex and soon become difficult to manage. Dividing it in components and using routing to load different components, helps in logically bifurcating \u00a0the app and making it more manageable.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Configuration<\/strong><\/p>\n<p>[code lang=&#8221;javascript&#8221;]<br \/>\nbower install react<br \/>\nbower install react-router<br \/>\n[\/code]<\/p>\n<pre><strong>Or if you are not using bower, use the following CDN links<\/strong>:<\/pre>\n<p>[code lang=&#8221;html&#8221;]<br \/>\nhttps:\/\/cdnjs.cloudflare.com\/ajax\/libs\/react\/0.13.3\/react.min.js<br \/>\nhttps:\/\/cdnjs.cloudflare.com\/ajax\/libs\/react\/0.13.3\/JSXTransformer.js<br \/>\nhttps:\/\/cdnjs.cloudflare.com\/ajax\/libs\/react-router\/0.13.3\/ReactRouter.min.js<br \/>\n[\/code]<\/p>\n<p>&nbsp;<\/p>\n<h2 style=\"text-align: center\">Setting up your page<\/h2>\n<p>[code lang=&#8221;html&#8221;]<br \/>\n&lt;!DOCTYPE html&gt;<br \/>\n&lt;html&gt;<br \/>\n&lt;head lang=&quot;en&quot;&gt;<br \/>\n    &lt;meta charset=&quot;UTF-8&quot;&gt;<br \/>\n    &lt;title&gt;&lt;\/title&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;script src=&quot;https:\/\/fb.me\/JSXTransformer-0.13.2.js&quot;\/&gt;<br \/>\n&lt;script src=&quot;https:\/\/fb.me\/react-with-addons-0.13.2.min.js&quot;\/&gt;<br \/>\n&lt;script src=&quot;bower_components\/react-router\/build\/global\/ReactRouter.min.js&quot;\/&gt;<br \/>\n&lt;script type=&quot;text\/jsx&quot; src=&quot;router.js&quot;\/&gt;<br \/>\n&lt;body&gt;<br \/>\n  &lt;div id=&quot;container&quot;&gt;&lt;\/div&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<br \/>\n[\/code]<\/p>\n<p>&nbsp;<\/p>\n<h2 style=\"text-align: center\">Basic example<\/h2>\n<p>[code lang=&#8221;javascript&#8221;]<br \/>\n(function (React, ReactRouter) {<br \/>\n    var Link = ReactRouter.Link,<br \/>\n        Route = ReactRouter.Route,<br \/>\n        RouteHandler = ReactRouter.RouteHandler;<br \/>\n    \/\/ List of Components<br \/>\n    var Parent = React.createClass({<br \/>\n        render: function () {<br \/>\n            return (<br \/>\n                &lt;div&gt;<br \/>\n                    &lt;h2&gt;This is the World.&lt;\/h2&gt;<br \/>\n                    &lt;ul&gt;<br \/>\n                        &lt;li&gt;<br \/>\n                            &lt;Link to=&#8217;india&#8217;&gt;India &lt;\/Link&gt;<br \/>\n                        &lt;\/li&gt;<br \/>\n                        &lt;li&gt;<br \/>\n                            &lt;Link to=&#8217;usa&#8217;&gt;USA&lt;\/Link&gt;<br \/>\n                        &lt;\/li&gt;<br \/>\n                    &lt;\/ul&gt;<br \/>\n                    &lt;RouteHandler\/&gt;<br \/>\n                &lt;\/div&gt;<br \/>\n            );<br \/>\n        }<br \/>\n    });<\/p>\n<p>    var India = React.createClass({<br \/>\n        render: function () {<br \/>\n            return (<br \/>\n                &lt;div&gt;<br \/>\n                India is a vast South Asian country with diverse terrain \u2013 from Himalayan peaks to Indian Ocean coastline \u2013 and history reaching back 5 millennia.<br \/>\n                In the north, Mughal Empire landmarks include Delhi\u2019s Red Fort complex, massive Jama Masjid mosque and Agra\u2019s iconic Taj Mahal mausoleum.<br \/>\n                    &lt;RouteHandler\/&gt;<br \/>\n                &lt;\/div&gt;<br \/>\n            );<br \/>\n        }<br \/>\n    });<br \/>\n    var USA = React.createClass({<br \/>\n        render: function () {<br \/>\n            return (<br \/>\n                &lt;div&gt;<br \/>\n                The Americas, or America are the combined continental landmasses of North America and South America.<br \/>\n                Along with their associated islands, they cover 8.3% of the Earth&#8217;s total surface area.<br \/>\n                    &lt;RouteHandler\/&gt;<br \/>\n                &lt;\/div&gt;<br \/>\n            );<br \/>\n        }<br \/>\n    });<\/p>\n<p>\/\/ Routing Information goes here<br \/>\n    var container = document.getElementById(&#8216;container&#8217;);<br \/>\n    var Routes = (<br \/>\n        &lt;Route  name=&quot;parent&quot; path=&quot;\/&quot; handler={Parent}&gt;<br \/>\n            &lt;Route name=&quot;india&quot; path=&quot;\/country\/india&quot; handler={India}\/&gt;<br \/>\n            &lt;Route name=&quot;usa&quot; path=&quot;\/country\/usa&quot; handler={USA} \/&gt;<br \/>\n        &lt;\/Route&gt;<br \/>\n    );<br \/>\n    ReactRouter.run(Routes, function (Handler) {<br \/>\n        React.render(&lt;Handler\/&gt;, container);<br \/>\n    });<br \/>\n}(window.React, window.ReactRouter));<br \/>\n[\/code]<\/p>\n<p><strong>Link<\/strong>: \u00a0Creates an anchor tag that links to a route in the application. Also gets the\u00a0<code>active<\/code> class automatically when the route matches. If you change the path of your route,\u00a0you don&#8217;t have to change your links.<\/p>\n<p>[code lang=&#8221;javascript&#8221;]<\/p>\n<p>  &lt;Link to=&#8217;india&#8217; params={{&quot;name&quot;: &quot;India&quot;}}&gt;India &lt;\/Link&gt;<\/p>\n<p> \/\/to:The name of the route to link to, or a full URL.<br \/>\n \/\/params: This props is used for passing parameter into url<br \/>\n \/\/query: It is used for passing parameter as query string into url<\/p>\n<p>[\/code]<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Route<\/strong>: It\u00a0map routes to your application&#8217;s screen hierarchy.<\/p>\n<p>[code lang=&#8221;javascript&#8221;]<br \/>\n &lt;Route  name=&quot;parent&quot; path=&quot;\/&quot; handler={Parent}&gt;<br \/>\n        &lt;Route name=&quot;india&quot; path=&quot;\/country\/india&quot; handler={India}\/&gt;<br \/>\n         &lt;Route name=&quot;usa&quot; path=&quot;\/country\/usa&quot; handler={USA} \/&gt;<br \/>\n &lt;\/Route&gt;<br \/>\n[\/code]<\/p>\n<p><strong>run()<\/strong>: It runs your routes, matching them against a location, and then calls back with the next state for you to render.<\/p>\n<p>[code lang=&#8221;javascript&#8221;]<br \/>\nReactRouter.run(Routes, function (Handler) {<br \/>\n        React.render(&lt;Handler\/&gt;,null);<br \/>\n    });<br \/>\n[\/code]<\/p>\n<p><strong>RouteHandler<\/strong>: Component that renders the active child route handler of a parent route handler component. Any time you have nested routes you need to render a <code>RouteHandler<\/code> to get the UI to nest.<\/p>\n<p>&nbsp;<\/p>\n<h2 style=\"text-align: center\">Passing parameters to Routes<\/h2>\n<p>[code lang=&#8221;javascript&#8221;]<br \/>\n&lt;div&gt;<br \/>\n   &lt;Link to=&#8217;india&#8217; params:{name:&quot;India&quot;}&gt;India &lt;\/Link&gt;<br \/>\n  \/\/Passing parameter in query string<br \/>\n   &lt;Link to=&#8217;india&#8217; query:{name:&quot;India&quot;}&gt;India &lt;\/Link&gt;<br \/>\n&lt;\/div&gt;<\/p>\n<p>\/\/Route is defined as<br \/>\n &lt;Route name=&quot;india&quot; path=&quot;\/country\/:name&quot; handler={India}\/&gt;<\/p>\n<p>[\/code]<\/p>\n<p>You can try it out yourself on JSFiddle <a title=\"ReactRouterDemo\" href=\"https:\/\/jsfiddle.net\/dhirajsharma072\/gk9s9pnv\/\" target=\"_blank\"> react-routerDemo <\/a>.<\/p>\n<p>To learn more\u00a0about react-router\u00a0visit\u00a0<a title=\"react-router\" href=\"https:\/\/github.com\/rackt\/react-router\" target=\"_blank\">react-router<\/a><\/p>\n<p>I&#8217;d love to get your feedback !!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>React-Router In this blog we will talk about react-router. Routing is a dire need in SPA(Single page applications), in which we load partial content without asking the server for a complete webpage. React is no different in terms of loading only what is required. Routing\u00a0programmatically presents specific content to users based on the URL that [&hellip;]<\/p>\n","protected":false},"author":95,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[1],"tags":[55,2048,2046],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23414"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/95"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=23414"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23414\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=23414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=23414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=23414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}