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

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

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

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离开页面,自定义弹框拦截,路由拦截》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

暗时间

暗时间

刘未鹏 / 电子工业出版社 / 2011-7 / 35.00元

2003年,刘未鹏在杂志上发表了自己的第一篇文章,并开始写博客。最初的博客较短,也较琐碎,并夹杂着一些翻译的文章。后来渐渐开始有了一些自己的心得和看法。总体上在这8年里,作者平均每个月写1篇博客或更少,但从未停止。 刘未鹏说—— 写博客这件事情给我最大的体会就是,一件事情如果你能够坚持做8年,那么不管效率和频率多低,最终总能取得一些很可观的收益。而另一个体会就是,一件事情只要你坚持得足......一起来看看 《暗时间》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

SHA 加密
SHA 加密

SHA 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器