react 路由跳转详情页 传递路由参数

1. 在App.js配置详情页面的路由

<Route path="/Detail/:id" component={Detail} />

2. 父页面,主要看页面里的routerTo()方法

import React, {Component} from 'react';
import {Link} from 'react-router-dom'
class Home extends Component {
  render() {
    var userList = [
      {
        id: 100,
        name: '林xx',
        age: 18
      }, {
        id: 104,
        name: '黄xx',
        age: 20
      }, {
        id: 106,
        name: '王xx',
        age: 30
      }
    ]
    return (
      <div>
        <Link to="/List">Home页面</Link>
        <ul>
          {
            userList.map(v => {
              return <li key={v.id}>
                <button onClick={()=>this.routerTo(v)}>跳转到详情页面</button>
              </li>
            })
          }
        </ul>
      </div>
    )
  }
  routerTo(v) {
    this.props.history.push({pathname: `/Detail/${v.id}`, state: {data: v}})
  }
}
export default Home

3. 详情页

import React, {Component} from 'react';

class Detail extends Component {
  constructor(props, context) {
    super(props, context)
    console.info(props)
    this.userList = props.location.state.data
  }
  render() {
    var userList = {}
    return (
      <div>
        <p>详情页面</p>
        <li>姓名:{this.userList.name}--年龄:{this.userList.age}--ID:{this.userList.id}</li>
      </div>
    )
  }
}

export default Detail;

 

posted @ 2018-08-24 16:41  LWJ_jay  阅读(15719)  评论(0编辑  收藏  举报