how to create dynamic route in react js with one component code example
Example 1: set dynamic route in link react js
<BrowserRouter>
{heroes.map(hero => (<Link to={'heroes/' + hero.id} />)}
<Route path="heroes/:id" component={Hero} />
</BrowserRouter>
class Hero extends Component {
render() {
return (
<div>
{this.props.match.params.id}
</div>
);
}
}
Example 2: react router dynamic routes
<Route path="/:id" render={(props) => (<Component {...props} />)} />
<Route path="/:id" component={Component} />