I have two doubts regarding usage of Route
from react-router-dom
(v4.3.1):
When do we use
component
vsrender
inRoute
:<Route exact path='/u/:username/' component={ProfileComponent} /> <Route exact path='/u/:username/' render={() => <ProfileComponent />} />
- How to access the variable
username
in the URL in both ways?
When you pass a component to the
component
prop, the component will get the path parameters in theprops.match.params
object, i.eprops.match.params.username
in your example:When using the
render
prop, the path parameters can be accessed through the props given to therender
function:You generally use the
render
prop when you need some data from the component that contains your routes, since thecomponent
prop gives no real way of passing in additional props to the component.