React 使用 this.context.router.push跳转传参以及接收方法

在使用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 : { name:'dd'}});
        } else if (noLogin == 0) {
            this.context.router.push({ pathname : '/login', state : { name:'aa' }});
        }
    }

在接收参数的页面用如下方法取出参数:


class LoginPage extends React.Component {
    constructor(props, context) {
        super(props, context);
    }
    render() {
        return (
            <div>
                {this.props.location.state}
            </div>
        );
    }
}
//index.contextTypes: {
 //       router: React.PropTypes.object
   // },

原文:https://blog.csdn.net/lvchengbo/article/details/78497161