Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set component key equal to path parameter in Route

I have a route defined as follows:

<Route exact path="/licenses/:type?" component={Licenses} />

I want my component to re-render when the type parameter changes, so, as mentioned in react-router documentation, I need to use a key. I want the value of key same as param passed to the route. Something like:

<Route exact path="/licenses/:type?" key=":type" component={Licenses} />

But I am unable to get the value of the type parameter as key. Is there any way to set the key same as type parameter's value?

like image 302
Vishal Avatar asked Jan 01 '23 14:01

Vishal


1 Answers

Got it!

I should use render instead of component like this:

<Route
  exact
  path="/licenses/:type?"
  render={props => <Licenses key={props.match.params.type || 'empty'} /> }
/>
like image 130
Vishal Avatar answered Jan 04 '23 04:01

Vishal