在使用React进行页面跳转传参的过程中,对于新手来说经常会遇到在接收参数的页面this.props.location is null or undefined的情况,对于接收的页面,一定要在constructor方法中添加props这个参数。
class index extends React.Component {
render() {
return (
<div>
home page
</div>
);
}
componentWillMount() {
if (noLogin == 1) {
this.context.router.push({ pathname : '/', state : { msg : 'you have logined and will redirect to your page'}});
} else if (noLogin == 0) {
this.context.router.push({ pathname : '/login', state : { msg : 'you do not login yet, please login' }});
}
}
}在接收参数的页面用如下方法取出参数:
class LoginPage extends React.Component {
constructor(props, context) {
super(props, context);
}
render() {
return (
<div>
{this.props.location.state}
</div>
);
}
}

本文介绍了在React应用中如何实现页面间的跳转并传递参数。重点讲解了如何避免接收参数页面this.props.location为null或undefined的问题,并给出了具体的代码实例。

3897

被折叠的 条评论
为什么被折叠?



