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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM