{
    "componentChunkName": "component---src-templates-blog-post-js",
    "path": "/react-tutorial-using-react-router6-typescript/",
    "result": {"data":{"site":{"siteMetadata":{"title":"CrewCode Solutions"}},"markdownRemark":{"id":"72a4892b-edf3-5513-90ce-f24179cc276d","excerpt":"In this article i will explain you step by step to use react router v6 with nested routes in react typescript project Step 1: Create react typescript project…","html":"<p>In this article i will explain you step by step to use react router v6 with nested routes in react typescript project</p>\n<h4>Step 1: Create react typescript project with following command</h4>\n<pre><code class=\"language-js\">npx create-react-app my-app --template typescript\n</code></pre>\n<h4>Step 2: Install react router dom v6</h4>\n<p>Install react router dom v6 in your newly created project using following command</p>\n<pre><code class=\"language-js\">npm install --save react-router-dom@6\n</code></pre>\n<h4>Step 3: Create Pages and component i will show you an example of Blog website</h4>\n<ul>\n<li>Create file <strong><em>src/pages/Blog.tsx</em></strong> and copy paste the following code</li>\n</ul>\n<pre><code class=\"language-js\">import React from \"react\";\nimport { Link, Routes, Route } from \"react-router-dom\";\nimport BlogCategoryWiseListing from \"../components/blogcategory\";\nimport AllBlogList from \"../components/bloglist\";\n\nconst Blog: React.FC&#x3C;{}> = () => {\n  return (\n    &#x3C;div className=\"album py-5 bg-light\">\n      &#x3C;div className=\"container\">\n        &#x3C;div className=\"row\">\n          &#x3C;div className=\"col-md-2\">\n            &#x3C;div className=\"list-group\" id=\"list-tab\" role=\"tablist\">\n              &#x3C;Link\n                className=\"list-group-item list-group-item-action active\"\n                id=\"list-home-list\"\n                data-bs-toggle=\"list\"\n                role=\"tab\"\n                aria-controls=\"list-home\"\n                to=\"/blog/javascript\"\n              >\n                Javascript\n              &#x3C;/Link>\n              &#x3C;Link\n                className=\"list-group-item list-group-item-action\"\n                id=\"list-home-list\"\n                data-bs-toggle=\"list\"\n                role=\"tab\"\n                aria-controls=\"list-home\"\n                to=\"/blog/php\"\n              >\n                Php\n              &#x3C;/Link>\n            &#x3C;/div>\n          &#x3C;/div>\n          &#x3C;div className=\"col-md-10\">\n            &#x3C;Routes>\n              &#x3C;Route path=\"/\" element={&#x3C;AllBlogList />} />\n              &#x3C;Route path=\"javascript\" element={&#x3C;BlogCategoryWiseListing />} />\n            &#x3C;/Routes>\n          &#x3C;/div>\n        &#x3C;/div>\n      &#x3C;/div>\n    &#x3C;/div>\n  );\n};\n\nexport default Blog;\n</code></pre>\n<ul>\n<li>Create file <strong><em>src/pages/Home.tsx</em></strong> and copy paste the following code</li>\n</ul>\n<pre><code class=\"language-js\">import React from \"react\";\n\nconst Home: React.FC&#x3C;{}> = () => {\n  return (\n    &#x3C;div>\n      &#x3C;h1>This is home component&#x3C;/h1>\n    &#x3C;/div>\n  );\n};\n\nexport default Home;\n</code></pre>\n<ul>\n<li>Create file <strong><em>src/pages/Login.tsx</em></strong> and copy paste the following code</li>\n</ul>\n<pre><code class=\"language-js\">import React from \"react\";\n\nconst Login: React.FC&#x3C;{}> = () => {\n  return (\n    &#x3C;div>\n      &#x3C;h1>This is login component&#x3C;/h1>\n    &#x3C;/div>\n  );\n};\n\nexport default Login;\n</code></pre>\n<ul>\n<li>Create file <strong><em>src/pages/SignUp.tsx</em></strong> and copy paste the following code</li>\n</ul>\n<pre><code class=\"language-js\">import React from \"react\";\n\nconst SignUp: React.FC&#x3C;{}> = () => {\n  return (\n    &#x3C;div>\n      &#x3C;h1>This is signup component&#x3C;/h1>\n    &#x3C;/div>\n  );\n};\n\nexport default SignUp;\n</code></pre>\n<ul>\n<li>Create file <strong><em>src/components/NavBar.tsx</em></strong> and copy paste the following code</li>\n</ul>\n<pre><code class=\"language-js\">import React from \"react\";\nimport { Link, useNavigate } from \"react-router-dom\";\n\nconst NavBar: React.FC&#x3C;{}> = () => {\n  const navigate = useNavigate();\n  return (\n    &#x3C;header className=\"p-3 bg-dark text-white\">\n      &#x3C;div className=\"container\">\n        &#x3C;div className=\"d-flex flex-wrap align-items-center justify-content-center justify-content-lg-start\">\n          &#x3C;div\n            onClick={() => {\n              navigate(\"/\");\n            }}\n            className=\"d-flex align-items-center mb-2 mb-lg-0 text-white text-decoration-none\"\n          >\n            &#x3C;h4>Routing Example v6&#x3C;/h4>\n          &#x3C;/div>\n          &#x3C;ul className=\"nav col-12 col-lg-auto me-lg-auto mb-2 justify-content-center mb-md-0\">\n            &#x3C;li>\n              &#x3C;Link to=\"/\" className=\"nav-link px-2 text-white\">\n                Home\n              &#x3C;/Link>\n            &#x3C;/li>\n            &#x3C;li>\n              &#x3C;Link to=\"/blog\" className=\"nav-link px-2 text-white\">\n                Blog\n              &#x3C;/Link>\n            &#x3C;/li>\n          &#x3C;/ul>\n          &#x3C;div className=\"text-end\">\n            &#x3C;Link\n              to=\"/signin\"\n              type=\"button\"\n              className=\"btn btn-outline-light me-2\"\n            >\n              Login\n            &#x3C;/Link>\n            &#x3C;Link to=\"/signup\" type=\"button\" className=\"btn btn-warning\">\n              Sign Up\n            &#x3C;/Link>\n          &#x3C;/div>\n        &#x3C;/div>\n      &#x3C;/div>\n    &#x3C;/header>\n  );\n};\n\nexport default NavBar;\n</code></pre>\n<ul>\n<li>Create file <strong><em>src/components/blogcategory.tsx</em></strong> and copy paste the following code</li>\n</ul>\n<pre><code class=\"language-js\">import React from \"react\";\n\nconst BlogCategoryWiseListing: React.FC&#x3C;{}> = () => {\n  return (\n    &#x3C;div>\n      &#x3C;h1>Blog Category Wise Listing&#x3C;/h1>\n    &#x3C;/div>\n  );\n};\n\nexport default BlogCategoryWiseListing;\n</code></pre>\n<ul>\n<li>Create file <strong><em>src/components/bloglist.tsx</em></strong> and copy paste the following code</li>\n</ul>\n<pre><code class=\"language-js\">import React from \"react\";\n\nconst AllBlogList: React.FC&#x3C;{}> = () => {\n  return (\n    &#x3C;div>\n      &#x3C;h1>All Blog listing&#x3C;/h1>\n    &#x3C;/div>\n  );\n};\n\nexport default AllBlogList;\n</code></pre>\n<h4>Step 3: Update App.tsx file and create routes to navigate to different pages</h4>\n<pre><code class=\"language-js\">import { BrowserRouter, Routes, Route } from \"react-router-dom\";\nimport NavBar from \"./components/NavBar\";\nimport Login from \"./pages/Login\";\nimport SignUp from \"./pages/SignUp\";\nimport Home from \"./pages/Home\";\nimport Blog from \"./pages/Blog\";\nimport AllBlogList from \"./components/bloglist\";\nimport BlogListingCategoryWise from \"./components/blogcategory\";\nimport \"./App.css\";\n\nfunction App() {\n  return (\n    &#x3C;div>\n      &#x3C;BrowserRouter>\n        &#x3C;NavBar />\n        &#x3C;div>\n          &#x3C;Routes>\n            &#x3C;Route path=\"/\" element={&#x3C;Home />} />\n            &#x3C;Route path=\"/signin\" element={&#x3C;Login />} />\n            &#x3C;Route path=\"/signup\" element={&#x3C;SignUp />} />\n            &#x3C;Route path=\"/blog\" element={&#x3C;Blog />}>\n              &#x3C;Route path=\"blog\" element={&#x3C;AllBlogList />} />\n              &#x3C;Route path=\"javascript\" element={&#x3C;BlogListingCategoryWise />} />\n            &#x3C;/Route>\n          &#x3C;/Routes>\n        &#x3C;/div>\n      &#x3C;/BrowserRouter>\n    &#x3C;/div>\n  );\n}\n\nexport default App;\n</code></pre>\n<p><strong><em>Nested routes defined for blog page</em></strong></p>\n<pre><code class=\"language-js\">&#x3C;Route path=\"/blog\" element={&#x3C;Blog />}>\n  &#x3C;Route path=\"blog\" element={&#x3C;AllBlogList />} />\n  &#x3C;Route path=\"javascript\" element={&#x3C;BlogCategory />} />\n&#x3C;/Route>\n</code></pre>","fields":{"slug":"/react-tutorial-using-react-router6-typescript/"},"frontmatter":{"title":"React tutorial using react router v6 in react typescript with nested routes","date":"April 04, 2023","description":"In this article i will explain you step by step to setup your react typescript project with react router v6","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/React.jpg"}},"previous":{"fields":{"slug":"/react-tutorial-using-react-portals-refs-typescript/"},"frontmatter":{"title":"React tutorial using react portal and refs in typescript","date":"March 30, 2023","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/React.jpg"}},"next":{"fields":{"slug":"/react-tutorial-using-redux-saga-typescript/"},"frontmatter":{"title":"React tutorial using redux saga in react typescript application","date":"April 16, 2023","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/React.jpg"}}},"pageContext":{"id":"72a4892b-edf3-5513-90ce-f24179cc276d","previousPostId":"bc5ca7cf-a9a9-51bf-a020-f308b4061a7a","nextPostId":"a48018e9-44ac-53f0-a03b-da9bde9fb047"}},
    "staticQueryHashes": ["3860684146"]}