react离开页面,自定义弹框拦截,路由拦截

栏目: 服务器 · 发布时间: 4年前

内容简介:前言:项目有个需求是:

react离开页面,自定义弹框拦截,路由拦截

前言:

项目有个需求是: 跳转路由,在离开页面前,需要弹框询问用户是否确定离开。

react-router<Prompt> 组件是可以的, 但是,怎么使用 antd 组件(或者说自定义组件)呢?

请看下面

先看的这个: https://stackoverflow.com/questions/32841757/detecting-user-leaving-page-with-react-router

(1)使用 react-router<Prompt>

import { Prompt } from 'react-router'

<Prompt
  message="你确定要离开嘛?"
/>

react离开页面,自定义弹框拦截,路由拦截

(2) <Prompt> 支持函数

<Prompt
  when={true}
  message={(location) => {
    return window.confirm(`confirm to leave to ${location.pathname}?`);
  }}
/>

react离开页面,自定义弹框拦截,路由拦截

(3) history.block这个是个坑!

import { history } from 'path/to/history';
class MyComponent extends React.Component {
   componentDidMount() {
      history.block(targetLocation => {
           // take your action here     
           return false;
      });
   }
   render() {
      //component render here
   }
}

坑的原因是 history.block() 方法是不能和 <Modal> 组件并列使用的,而必须在 history.block() 内部去调用 <Modal> 组件(就是注释 take your action here 那里),这就导致一个问题: <Modal> 组件里的 onOk、onCancel 如何返回值给 history.block() 的 return 语句(return false/true)的同时,不让 history.block() 的 return 语句和 <Modal> 组件顺序执行呢?

说白点就是, <Modal> 组件先显示出来,阻塞后面的 return false/true,等 <Modal> 组件的 onOk、onCancel 方法执行后,再 return false/true

我试了几种方法,不行,直到找到这篇文章:

Using React-Router v4 Prompt with custom modal component

(4)在离开页面,路由跳转时,自定义弹框拦截,并询问

handlePrompt = location => {
    if (!this.isSave) {
      this.showModalSave(location);
      return false;
    }
    return true;
  };

  showModalSave = location => {
    this.setState({
      modalVisible: true,
      location,
    });
  };

  closeModalSave = () => {
    const { location } = this.state;
    const { history } = this.props;
    this.setState({
      modalVisible: false,
    });
    history.push({
      pathname: `..${location.pathname}`,
    });
  };

  handlePrompt = location => {
    if (!this.isSave) {
      this.showModalSave(location);
      return false;
    }
    return true;a
  };

  handleSave = () => {
    const { location } = this.state;
    const { history } = this.props;
    this.isSave = true;
    console.log(location.pathname, 'pathname75');
    history.push({
      pathname: `..${location.pathname}`,
    });
    this.setState({
      modalVisible: false,
    });
    this.saveAll();
  };

 <Prompt message={this.handlePrompt}/>
<Modal title="提示"
       visible={modalVisible}
       onCancel={this.closeModalSave}
       closable={false}
       centered
       footer={null}
>
<div>是否保存修改?</div>
<div>
      <Button onClick={this.closeModalSave}>
            不保存
      </Button>
       <Button onClick={this.handleSave}>
            保存
       </Button>
</div>
</Modal>

react离开页面,自定义弹框拦截,路由拦截

完美实现离开页面,路由拦截的同时,显示自定义模态框!

react离开页面,自定义弹框拦截,路由拦截

(完)


以上所述就是小编给大家介绍的《react离开页面,自定义弹框拦截,路由拦截》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

智能

智能

[法]弗雷德里克·马特尔 / 君瑞图、左玉冰 / 商务印书馆 / 2015-8 / 59.90

揭示数字化主流大趋势的最新权威论著 《主流》作者的最新力作!与法国秋季新版同步上市! 面对数字化时代的到来,美国、欧盟、中国、俄罗斯、印度、巴西、古巴、伊朗、南非、韩国、新加坡、肯尼亚、墨西哥、日本等世界各国各地区正在如何应对? 在国际地缘政治格局下如何把握数字化主流的大趋势? 谷歌、苹果、脸书、亚马逊、阿里巴巴、腾讯、中兴、华为等大家熟知的网络巨头接受了作者的采访。作者的......一起来看看 《智能》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

随机密码生成器
随机密码生成器

多种字符组合密码