react 跳转页面

第一种:this.props.router.push(path)

第二种:this.context.router.push(path)

文件要额外添加一些内容

class Example extends React.Component {
    constructor(props, context) {
        super(props, context);
        this.context.router; // it works
    }
	...
    this.context.router.push(path);
}

Example.contextTypes =  {
    router: React.PropTypes.object
};
class index extends React.Component {  
    render() {  
        return (  
            
home page
); } 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 (  
            
{this.props.location.state}
); } }




你可能感兴趣的:(react)