代码
// 页面 1
<Link
key={value}
to={{
pathname: `/test/{record.id}`,
state: {
id: record.id,
},
}}
>
{value}
</Link>
// 页面 2
console.log(this.props.location.state.id)
直接访问 /test/1 时,location.state 全部丢失。
当页面跳转需要传递一些参数时,使用 match.params。 例子🌰:
// route 定义
<Route path="/path/:id" component={Page2}>
// Page1 中做页面跳转
<Link to="/path/123">
// Page2 中获取参数
const { id } = this.props.match.params