在 App.js 中
<Route exact path='/page/:id' component={Dashboard} />
在 Dashboard.js 中,我可以获取 id,但是如何在不每次都编写的情况下进一步获取它?
仪表板.js:
import React, { Component } from 'react'
import PokemonList from '../pokemon/PokemonList'
export default class Dashboard extends Component {
render() {
const id = this.props.match.params.id;
return (
<div >
<PokemonList id={id} />
</div>
)
}
}
有没有更好的办法?然后我也必须继续转发它。
自该
react-router版本以来2.7,添加了一个更高阶的组件 (HOC) - withRouter所有需要访问参数的组件,都包含在
withRouter(更多)中-然后您将在道具中获得所需的内容ES7:
ES6: