In this lesson we'll show how to take a beforeUnload call and convert it to a declarative React Component. It will handle subscribing to events, and automatically unsubscribing to prevent memory leaks.

class BeforeUnload extends React.Component {

  constructor(props) {
super(props);
this.alertMessage = this.alertMessage.bind(this);
} componentDidMount() {
window.addEventListener("beforeunload", this.alertMessage);
} componentDidUpdate(prevProps, prevState) {
const { active } = this.props;
const { active: wasActive } = prevProps; if (wasActive && !active) {
window.removeEventListener("beforeunload", this.alertMessage);
} else if (!wasActive && active) {
window.addEventListener("beforeunload", this.alertMessage);
}
} componentWillUnmount() {
window.removeEventListener("beforeunload", this.alertMessage);
} alertMessage(e) {
if (this.props.active) {
e.returnValue = true;
return true;
}
} render() {
return this.props.children;
}
} class App extends React.Component { constructor(props) {
super(props);
this.state = {
active: true,
}
this.toggle = this.toggle.bind(this);
} toggle() {
this.setState((state) => {
return { active: !state.active }
}
);
} render() {
return (
<BeforeUnload active={this.state.active}>
<button onClick={this.toggle}>{this.state.active ? "Active": "Inactive"}</button>
</BeforeUnload>
);
}
} ReactDOM.render(
<App />,
document.getElementById('example')
);

[React] Setup 'beforeunload' listener的更多相关文章

  1. React——教程 && 零基础入门 && 从实践中学习(待续)

    Tutorial: Intro to React This tutorial doesn’t assume any existing React knowledge. Tip This tutoria ...

  2. 二、react开发环境配置与webpack入门

    Webpack 模块打包工具(module bundler)功能: 将 CSS.图片与其他资源打包 打包之前预处理(Less.CoffeeScript.JSX.ES6 等)档案 依 entry 文件不 ...

  3. Google地图接口API之地图控件集(五)

    1.默认控件集 当使用一个标准的google地图,它的控件默认设置如下: (1). Zoom-显示一个滑动条来控制map的Zoom级别,如下所示:

  4. kali客户端攻击

    浏览器攻击 browser_autpwn2 (BAP2) mkdir /test 为接受响应的服务器创建目录   use auxiliary/server/browser_autopwn2  set ...

  5. New UWP Community Toolkit - Markdown

    概述 前面 New UWP Community Toolkit 文章中,我们对 V2.2.0 版本的重要更新做了简单回顾,其中简单介绍了 MarkdownTextBlock 和 MarkdownDoc ...

  6. 聊聊jstack的工作原理

    实现一个jstack 在聊Jstack得工作原理前呢,不如让我们先写一个简单的jstack玩玩.不用怕,很简单的,就几行代码的事,看: public class MyJstack { public s ...

  7. Kali-linux使用SET实施攻击

    前面介绍了社会工程学工具包(SET)的简单使用.为了能帮助用户更容易的理解社会工程学的强大功能.本节将介绍使用社会工程学工具包实施各种攻击. 7.4.1 针对性钓鱼攻击向量 针对性钓鱼攻击向量通过构造 ...

  8. ESP32 LyraT音频开发板试玩(二):播放音乐

    我是卓波,很高兴你来看我的博客. 系列文章: ESP32 LyraT音频开发板试玩(一):搭建开发环境 ESP32 LyraT音频开发板试玩(二):播放音乐 本文延续上一篇博客 将D:\msys32\ ...

  9. WebSocket 网页聊天室

    先给大家开一个原始的websocket的连接使用范例 <?php /* * recv是从套接口接收数据,也就是拿过来,但是不知道是什么 * read是读取拿过来的数据,就是要知道recv过来的是 ...

随机推荐

  1. (JavaScript基础向)日常小发现:forEach等函数的第二个参数的用法

    forEach函数用得平时用得比较多,但是从来没想到forEach函数还有第二个参数. 这里是菜鸟教程对forEach函数的详细说明:forEach的详细说明. 如上图,forEach函数有第二个参数 ...

  2. layui动态无限极菜单

    ajax加jQuery实现 效果图 参考文章:https://www.wanpishe.top/detail?blogId=644aa177-9795-456a-8090-ee1264bf5d9d

  3. RPC简易学习

    0.RPC简介 RPC,   英文全称:Remote Process Call.   中文全称:远程过程调用. 客户端通过网络请求调用远程服务端对外暴露服务.常用的两种RPC协议:TCP.HTTP. ...

  4. windows查看硬件信息系统信息

    rem 查看cpu wmic cpu list briefrem 查看物理内存wmic memphysical list briefrem 查看逻辑内存wmic memlogical list bri ...

  5. mapper提示Could not autowire. No beans of … type found?

    工具及背景: IntelliJ IDEA 2016.1.3 Ultimate.spring boot, maven项目,利用mybatis 注解的方式查询mysql 在自动生成工具生成代码后,serv ...

  6. MVC—实现ajax+mvc异步获取数据

    之前写过ajax和一般处理程序的结合实现前后台的数据交换的博客,如今做系统用到了MVC,同一时候也用到了异步获取数据. ajax+一般处理程序与MVC+ajax原理是一样的在"URL&quo ...

  7. Trafodion:Transactional SQL on HBase

    Trafodion: Transactional SQL on HBase HBase上实时分布式事务处理 介绍 HBase的SQL能力一直不足.Phoenix缺乏Join能力,eBay提出的kyli ...

  8. java.lang.ClassNotFoundException: org.springframework.web.content.ContextLoaderListener

    1.错误描写叙述 严重: Error configuring application listener of class org.springframework.web.content.Context ...

  9. Android学习笔记进阶21之设置壁纸

    别忘记在ApplicationManifest.xml 中加上权限的设置. <uses-permission Android:name = "android.permission.SE ...

  10. InstallShield详细制作说明(四)

    十.编译打包