{"id":2461,"date":"2023-02-20T08:51:37","date_gmt":"2023-02-20T08:51:37","guid":{"rendered":"https:\/\/codeinsightacademy.com\/blog\/?p=2461"},"modified":"2023-02-20T12:01:41","modified_gmt":"2023-02-20T12:01:41","slug":"routing-2","status":"publish","type":"post","link":"https:\/\/codeinsightacademy.com\/blog\/react\/routing-2\/","title":{"rendered":"Routing"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>npm i react-router-dom <\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>npx create-react-app routingapp<\/code><\/pre>\n\n\n\n<p>App.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import Home from '.\/components\/Home';\nimport Blogs from '.\/components\/Blogs';\nimport Contact from '.\/components\/Contact';\nimport NoPage from '.\/components\/NoPage';\nimport { BrowserRouter, Outlet, Route, Routes } from 'react-router-dom';\nimport Layout from '.\/components\/Layout';\n\nfunction App() {\n  return (\n    &lt;&gt;\n    &lt;h1&gt;HELLO WORLD&lt;\/h1&gt;\n    &lt;BrowserRouter&gt;\n      &lt;Routes&gt;\n        &lt;Route path=\"\/admin\" element={&lt;Layout \/&gt;}&gt;\n          &lt;Route index element={&lt;Home \/&gt;} \/&gt;\n          &lt;Route path=\"contact\" element={&lt;Contact \/&gt;} \/&gt;\n          &lt;Route path=\"*\" element={&lt;NoPage \/&gt;} \/&gt;\n        &lt;\/Route&gt;\n        &lt;Route path=\"\/user\" element={&lt;Layout \/&gt;}&gt;\n          &lt;Route index element={&lt;Home \/&gt;} \/&gt;\n          &lt;Route path=\"contact\" element={&lt;Contact \/&gt;} \/&gt;\n          &lt;Route path=\"blogs\" element={&lt;Blogs \/&gt;} \/&gt;\n          &lt;Route path=\"test\" element={&lt;div&gt;This is test component&lt;\/div&gt;} \/&gt;\n          &lt;Route path=\"*\" element={&lt;NoPage \/&gt;} \/&gt;    \n        &lt;\/Route&gt;\n      &lt;\/Routes&gt;\n    &lt;\/BrowserRouter&gt;\n    &lt;\/&gt;\n\n  );\n}\n\nexport default App;\n<\/code><\/pre>\n\n\n\n<p>Layout.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { Outlet, Link } from \"react-router-dom\";\n\nexport default function Layout() {\n  return (\n    &lt;&gt;\n      &lt;nav&gt;\n        &lt;ul&gt;\n          &lt;li&gt;\n            &lt;Link to=\"\/\"&gt;Home&lt;\/Link&gt;\n          &lt;\/li&gt;\n          &lt;li&gt;\n            &lt;Link to=\"\/blogs\"&gt;Blogs&lt;\/Link&gt;\n          &lt;\/li&gt;\n          &lt;li&gt;\n            &lt;Link to=\"\/contact\"&gt;Contact&lt;\/Link&gt;\n          &lt;\/li&gt;\n        &lt;\/ul&gt;\n      &lt;\/nav&gt;\n      &lt;Outlet \/&gt;\n    &lt;\/&gt;\n  )\n}\n<\/code><\/pre>\n\n\n\n<p>Outlet is the block where router components get loaded<\/p>\n","protected":false},"excerpt":{"rendered":"<p>App.js Layout.js Outlet is the block where router components get loaded<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[35],"tags":[],"_links":{"self":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/2461"}],"collection":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=2461"}],"version-history":[{"count":3,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/2461\/revisions"}],"predecessor-version":[{"id":2465,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/2461\/revisions\/2465"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=2461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=2461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=2461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}