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;
取数据 : const data = this.props.location.state;