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. Mysql学习总结(16)——Mysql之数据库设计规范

    一.三大范式 1.第一范式:消除一个字段包含多个数据库值,消除一个记录包含重复的组(单独的一列包含多个项目),即可满足1NF. 2.第二范式:消除部分依赖性即可转化为2NF.部分依赖性表示一个记录中包 ...

  2. Spring项目用junit 时出现org.junit.runners.BlockJUnit4ClassRunner cannot be resolved(转)

    spring框架项目用junit做测试时,程序在自动编译时出现下述问题: 程序的问题是项目中找不到org.junit.runners.BlockJUnit4ClassRunner,有两种可能,一是没有 ...

  3. c、c++ 结构体的嵌套

    c.c++ 结构体的嵌套 /************************************************************************/ /* 嵌套结构体 * C ...

  4. Android开发经验之点击图片判断是否在图片范围之内

    package xiaosi.grivaty; import android.content.Context; import android.graphics.Bitmap; import andro ...

  5. linux/unix 基本概念的认识(sha-bang 、PPA)

    PPA:Personal Package Archives : Ubuntu: 比如为安装 emacs,需要首先添加某个PPA: sudo add-apt-repository ppa:cassou/ ...

  6. 26.多线程join detach

    #include <iostream> #include <thread> #include <array> #include <Windows.h> ...

  7. 在VS中设置比较和谐的字体和颜色的方法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 先在studiostyl.es网站选择你喜欢的字体方案,我个人比较喜欢这款: Humane Studio,注意在网页上 ...

  8. JAVA 多线程和进程概念的引入

    1.并发和并行 并行:指两个或多个时间在同一时刻发生(同时发生): 并发:指两个或多个事件在一个时间段内发生. 在操作系统中,安装了多个程序,并发指的是在一段时间内宏观上有多个程序同时运行,这在单 C ...

  9. Linux shell command学习笔记(一)

    Shell的种类有很多种,例如CSH,Bourne Shell,Korn Shell.在现在的大多数Linux发行版中,默认的Shell一般都是Bourne again shell(bash). &l ...

  10. canvas:动态时钟

    此时针是以画布的中心为圆心: ctx.translate(width/2,width/2); 此函数是将画布的原点移到(width/2,width/2) 数字的位置我们利用了三角函数的原理 x=rco ...