{"id":11065,"date":"2024-08-06T04:57:42","date_gmt":"2024-08-06T04:57:42","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=11065"},"modified":"2024-08-06T04:57:42","modified_gmt":"2024-08-06T04:57:42","slug":"dynamic-route-issues-for-individualcard-in-react-router","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/react\/dynamic-route-issues-for-individualcard-in-react-router","title":{"rendered":"React Router Not Rendering Dynamic Route Correctly For IndividualCard Component"},"content":{"rendered":"<p>Dynamic route configuration:<\/p>\n<h3>1. Router Setup<\/h3>\n<p>BrowserRouter (aliased as Router): Wraps the entire application to enable React Router for navigation.<br \/>\nRoutes and Route: Define the route structure. The path prop in the Route component includes a dynamic segment :cardName, which captures any value in that part of the URL.<\/p>\n<h3>2. Extracting and Using the Dynamic Parameter:<\/h3>\n<p>useParams(): Extracts the cardName parameter from the URL.<\/p>\n<h2>Example:<\/h2>\n<p>Dynamic routing in this code allows the IndividualCard component to render based on the URL parameter cardName. By using the :cardName parameter in the route path and extracting it with useParams, the app can dynamically display content for different tarot cards. This implementation currently supports the tarot cards &#8220;the-magician&#8221; and &#8220;the-fool&#8221;, which can be accessed via \/introduction\/the-magician and \/introduction\/the-fool URLs, respectively.<\/p>\n<p><b>App.js<\/b><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">import \".\/styles.css\";\r\nimport { BrowserRouter as Router, Route, Routes } from \"react-router-dom\";\r\nimport IndividualCard from \".\/IndividualCard\";\r\nimport Home from \".\/Home\";\r\n\r\nexport default function App() {\r\n return (\r\n   &lt;Router&gt;\r\n     &lt;Routes&gt;\r\n       &lt;Route path=\"\/\" element={&lt;Home \/&gt;} \/&gt;\r\n       &lt;Route path=\"\/introduction\/:cardName\" element={&lt;IndividualCard \/&gt;} \/&gt;\r\n     &lt;\/Routes&gt;\r\n   &lt;\/Router&gt;\r\n );\r\n}\r\n<\/pre>\n<p><strong>IndividualCard.js<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">\r\nimport React from \"react\";\r\nimport { useParams } from \"react-router-dom\";\r\n\r\nexport default function IndividualCard() {\r\n  const { cardName } = useParams();\r\n\r\n  \/\/ Placeholder data for each Tarot card\r\n  const tarotCardData = {\r\n    \"the-fool\": {\r\n      image: \"\/tarot-placeholder.jpg\",\r\n      title: \"The Fool\",\r\n      description: \"Placeholder description for The Fool tarot card...\",\r\n      keywords: \"The Fool's Keywords\"\r\n    },\r\n    \"the-magician\": {\r\n      image: \"\/tarot-placeholder.jpg\",\r\n      title: \"The Magician\",\r\n      description: \"Placeholder description for The Magician tarot card...\",\r\n      keywords: \"The Magician's Keywords\"\r\n    }\r\n    \/\/ Add more cards as needed\r\n  };\r\n  const cardData = tarotCardData[cardName];\r\n  const {\r\n    description,\r\n    title\r\n  } = cardData;\r\n\r\n  return (\r\n    <div className=\"background_image\">\r\n      <div> {title} <\/div>\r\n      <div> {description} <\/div>\r\n    <\/div>\r\n  );\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Dynamic route configuration: 1. Router Setup BrowserRouter (aliased as Router): Wraps the entire application to enable React Router for navigation. Routes and Route: Define the route structure. The path prop in the Route component includes a dynamic segment :cardName, which captures any value in that part of the URL. 2. Extracting and Using the Dynamic [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":11066,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11065","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11065"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=11065"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11065\/revisions"}],"predecessor-version":[{"id":11068,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11065\/revisions\/11068"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/11066"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=11065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=11065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=11065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}