Manjushaka / learn-react

学习react相关的技术栈,包括react,webpack,redux等。
0 stars 0 forks source link

react-router页面跳转 #1

Open Manjushaka opened 6 years ago

Manjushaka commented 6 years ago

关于页面跳转的函数勾子。 适用场景,编辑页面有改动,需要确认返回的时候。

  componentWillMount() {
    this.unregisterLeaveHook = this.props.router.setRouteLeaveHook(
      this.props.route,
      this.routerWillLeave,
    );
    this.hasConfirmGoBack = false;
  }

  componentWillUnmount() {
    this.unregisterLeaveHook();
  }

  routerWillLeave = nextLocation => {
    // 有新的输入或改变,提醒是否继续返回
    if (this.contentForm.props.form.isFieldsTouched() && !this.hasConfirmGoBack) {
      confirm({
        title: '提示:',
        content: '您正在编辑的报告尚未保存,是否确认返回?',
        onOk: () => {
          this.hasConfirmGoBack = true;
          browserHistory.push(`${nextLocation.pathname}${nextLocation.search}`);
        },
      });

      return false;
    }

    return true;
  }