在使用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>
);
}
}