将组件键设置为等于 Route 中的路径参数

Vis*_*hal 3 javascript reactjs react-router

我有一个定义如下的路线:

<Route exact path="/licenses/:type?" component={Licenses} />
Run Code Online (Sandbox Code Playgroud)

我希望我的组件在类型参数更改时重新渲染,因此,如 react-router 文档中所述,我需要使用一个键。我想要与传递给路由的参数相同的键值。就像是:

<Route exact path="/licenses/:type?" key=":type" component={Licenses} />
Run Code Online (Sandbox Code Playgroud)

但是我无法获取类型参数的值作为键。有没有办法将键设置为与类型参数的值相同?

Vis*_*hal 8

知道了!

我应该使用render而不是component这样:

<Route
  exact
  path="/licenses/:type?"
  render={props => <Licenses key={props.match.params.type || 'empty'} /> }
/>
Run Code Online (Sandbox Code Playgroud)