Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

reactjs dynamic route

/* Links */
{heroes.map(hero => (<Link to={'heroes/' + hero.id} />)}
              
<Router>
  /* Component */
  <Route path="heroes/:id" component={Hero} />
</Router>

import { useParams } from "react-router-dom";
const Hero = () => {
    const params = useParams();
    // params.id => dynamic value defined as id in route
    // e.g '/heroes/1234' -> params.id equals 1234
    return (...)
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #reactjs #dynamic #route
ADD COMMENT
Topic
Name
7+3 =