[React] Setup 'beforeunload' listener
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的更多相关文章
- React——教程 && 零基础入门 && 从实践中学习(待续)
Tutorial: Intro to React This tutorial doesn’t assume any existing React knowledge. Tip This tutoria ...
- 二、react开发环境配置与webpack入门
Webpack 模块打包工具(module bundler)功能: 将 CSS.图片与其他资源打包 打包之前预处理(Less.CoffeeScript.JSX.ES6 等)档案 依 entry 文件不 ...
- Google地图接口API之地图控件集(五)
1.默认控件集 当使用一个标准的google地图,它的控件默认设置如下: (1). Zoom-显示一个滑动条来控制map的Zoom级别,如下所示:
- kali客户端攻击
浏览器攻击 browser_autpwn2 (BAP2) mkdir /test 为接受响应的服务器创建目录 use auxiliary/server/browser_autopwn2 set ...
- New UWP Community Toolkit - Markdown
概述 前面 New UWP Community Toolkit 文章中,我们对 V2.2.0 版本的重要更新做了简单回顾,其中简单介绍了 MarkdownTextBlock 和 MarkdownDoc ...
- 聊聊jstack的工作原理
实现一个jstack 在聊Jstack得工作原理前呢,不如让我们先写一个简单的jstack玩玩.不用怕,很简单的,就几行代码的事,看: public class MyJstack { public s ...
- Kali-linux使用SET实施攻击
前面介绍了社会工程学工具包(SET)的简单使用.为了能帮助用户更容易的理解社会工程学的强大功能.本节将介绍使用社会工程学工具包实施各种攻击. 7.4.1 针对性钓鱼攻击向量 针对性钓鱼攻击向量通过构造 ...
- ESP32 LyraT音频开发板试玩(二):播放音乐
我是卓波,很高兴你来看我的博客. 系列文章: ESP32 LyraT音频开发板试玩(一):搭建开发环境 ESP32 LyraT音频开发板试玩(二):播放音乐 本文延续上一篇博客 将D:\msys32\ ...
- WebSocket 网页聊天室
先给大家开一个原始的websocket的连接使用范例 <?php /* * recv是从套接口接收数据,也就是拿过来,但是不知道是什么 * read是读取拿过来的数据,就是要知道recv过来的是 ...
随机推荐
- [Python] Python's namedtuples can be a great alternative to defining a class manually
# Why Python is Great: Namedtuples # Using namedtuple is way shorter than # defining a class manuall ...
- Qt 5.11的QChar、QString、QTextBoundaryFinder和双向文本算法现在完全兼容Unicode 10
本文翻译自:Qt 5.11 released 原文作者: Qt公司CTO兼Qt开源项目维护官Lars Knoll翻译校审:Richard.Hongfei.Haipeng 5月22日,我们提发布了Qt ...
- ONVIF客户端搜索设备获取rtsp地址开发笔记(精华篇)
原文 http://blog.csdn.net/gubenpeiyuan/article/details/25618177 概要: 目前ONVIF协议家族设备已占据数字监控行业半壁江山以上,亲, ...
- Flume的data flow(数据流)
data flow描述了数据从产生,传输.处理并最终写入目标的一条路径. 数据的采集的流向!如下图所示.
- Appium_pytest fixture的使用
一.定义fixture方法 # -*- coding:utf-8 -*-import pytestfrom baseutil.DriverUtil import DriverConfig @pytes ...
- STM32之CAN通讯接收过滤器过滤分析
一.前言 学习了CAN通讯,底层的东东CAN控制器已经帮你处理完成,也就是CAN通讯协议已经做好,你按协议格式往对应的位扔数据发送就好,所以使用CAN通讯,我们只需要去关心制定发送的数据间的协议,也就 ...
- iOS关闭键盘简单实现(objc/swift)
Objective-C 代码实例方式一 [[[UIApplication sharedApplication] keyWindow] endEditing:YES]; 假设一个view上有很多Text ...
- 1.13 Python基础知识 - 字典和集合
一.字典 字典是一组键-值对的数据结构.每个键对应一个值.在字典中,键不能重复.根据键可以查询到值.字典是键和值的映射关系 字典的定义: 字典通过花括号中用逗号分隔的元素(键-值.键-值对使用冒号分隔 ...
- 1.10 Python基础知识 - 序列:列表
在Python中有很多的组合数据类型,其中包括列表,元组,字符串等数据类型,这些数据类型统称为序列类型,用他们可以处理复杂的数据. 列表,是一组有序元素组合的数据结构.列表是可变的数据类型. 列表采用 ...
- 15个常用的javaScript正则表达式--来自于javascript公众号
摘要 收集整理了15个常用的javaScript正则表达式,其中包括用户名.密码强度.整数.数字.电子邮件地址(Email).手机号码.身份证号.URL地址. IPv4地址. 十六进制颜色. 日期. ...