1、配置动态路由

App.js

  1. <Switch>
  2. <Route exact path="/" component={Home}></Route>
  3. <Route path="/about" component={About}></Route>
  4. <Route path="/detail/:id" component={Detail}></Route>
  5. </Switch>

2、跳转

Home.js

  1. class Home extends Component {
  2. constructor(props){
  3. super(props);
  4. this.state={
  5. id:1001
  6. }
  7. }
  8. render() {
  9. return (
  10. <div>
  11. 主页
  12. <Button onClick={this.handleToggle}>detail</Button>
  13. </div>
  14. );
  15. }
  16. handleToggle=()=>{
  17. this.props.history.push(`/detail/${this.state.id}`)
  18. }
  19. }

3、详情页接收

this.props.match.params

  1. class Detail extends Component {
  2. render() {
  3. return (
  4. <div>
  5. 详情页
  6. </div>
  7. );
  8. }
  9. componentDidMount() {
  10. console.log(this.props.match.params)
  11. }
  12. }