表单验证实现React-router跳转

Posted on 2018-04-27 15:48  郑小胖  阅读(225)  评论(0编辑  收藏  举报

方法一:broserHistory.push

  handleSubmit(e){

    e.preventDefault();

    const path = '/demo';

    broserHistory.push(path); 

  }

方法二:context对象方法

  export default React.createClass({

    // ask for `router` from context

    contextTypes: {

      router: React.PropTypes.object

    },

    handleSubmit(event) {

      // ...

      this.context.router.push(path)

    },

  })

方法三:this.props.history

  submitForm(e) {

    e.preventDefault();
    const _this = this;
    _this.refs.ruleForm.validate((valid) => {
      if (valid) {
        _this.props.history.push('./../pages/demo');
      } else {
        console.log('error submit!!');
      }
    });
  
}