[ReactJS] DOM Event Listeners in a React Component
React doesn't provide the listener to listen the DOM event. But we can do it in React life cycle:
So when the compoment did mount, we add listeners to the dom event.
And remember to remove the dom listener when the compoment unmount.
var Box = React.createClass({
getInitialState:function(){
return {
width: window.innerWidth,
scroll: document.body.scrollTop
};
},
update: function(){
this.setState({
width: window.innerWidth,
scroll: document.body.scrollTop
})
},
componentDidMount:function(){
window.addEventListener('resize', this.update);
widnow.addEventListener('scroll', this.update);
},
componentWillUnmount:function(){
window.removeEventListener('resize', this.update);
window.removeEventListener('scroll', this.update);
},
render:function(){
return <div>
<p>width: {this.state.width}</p>
<p>scroll: {this.state.scroll}</p>
</div>;
}
});
//React.render will be depricated in the next release
//https://facebook.github.io/react/blog/2015/09/10/react-v0.14-rc1.html#two-packages-react-and-react-dom
ReactDOM.render(<Box />, document.getElementById('box'));
[ReactJS] DOM Event Listeners in a React Component的更多相关文章
- React.js Tutorial: React Component Lifecycle
Introduction about React component lifecycle. 1 Lifecycle A React component in browser can be any of ...
- 学习React系列(一)——React.Component 生命周期
挂载中(只执行一次) 以下方法在组件实例正被创建和插入到DOM中时调用 constructor()一般用于初始化state和方法的this绑定 componentWillMount() render( ...
- React.Component(V16.8.6)
组件的生命周期 挂载 当组件实例被创建并插入 DOM 中时,其生命周期调用顺序如下: constructor() static getDerivedStateFromProps() render() ...
- [DOM Event Learning] Section 4 事件分发和DOM事件流
[DOM Event Learning] Section 4 事件分发和DOM事件流 事件分发机制: event dispatch mechanism. 事件流(event flow)描述了事件对象在 ...
- [DOM Event Learning] Section 3 jQuery事件处理基础 on(), off()和one()方法使用
[DOM Event Learning] Section 3 jQuery事件处理基础 on(),off()和one()方法使用 jQuery提供了简单的方法来向选择器(对应页面上的元素)绑定事件 ...
- 让页面滑动流畅得飞起的新特性:Passive Event Listeners
版权声明:本文由陈志兴原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/153 来源:腾云阁 https://www.qclo ...
- HTML: Dom event
转自:https://developer.mozilla.org/zh-CN/docs/Web/API/Event Event接口表示在DOM中发生的任何事件; 一些是用户生成的(例如鼠标或键盘事件) ...
- 002-and design-dva.js 知识导图-01JavaScript 语言,React Component
一.概述 参看:https://github.com/dvajs/dva-knowledgemap react 或 dva 时会不会有这样的疑惑: es6 特性那么多,我需要全部学会吗? react ...
- [Mobx] Using mobx to isolate a React component state
React is great for diffing between Virtual-DOM and rendering it to the dom. It also offers a naïve s ...
随机推荐
- io系统
一.浅谈io系统 io系统的结构化思想是:输入-转换流-装饰器-输出. 对于字节流来说,常见的结构类为: package com.handchina.yunmart.middleware.servic ...
- RAC 的一些概念性和原理性的知识(转)
一 集群环境下的一些特殊问题 1.1 并发控制 在集群环境中, 关键数据通常是共享存放的,比如放在共享磁盘上. 而各个节点的对数据有相同的访问权限, 这时就必须有某种机制能够控制节点对数据的访问. O ...
- iOS应用性能调优的25个建议和技巧【转】
转载自:http://blog.jobbole.com/37984/ 首页 最新文章 资讯 程序员 设计 IT技术 创业 在国外 营销 趣文 特别分享 更多 > - Navigation - ...
- 深入解析MySQL replication协议
Why 最开始的时候,go-mysql只是简单的抽象mixer的代码,提供一个基本的mysql driver以及proxy framework,但做到后面,笔者突然觉得,既然研究了这么久mysql c ...
- 只包含schema的dll生成和引用方法
工作中,所有的tools里有一个project是只包含若干个schema的工程,研究了一下,发现创建这种只包含schema的dll其实非常简单. 首先,在visual studio-new proje ...
- Katana概述
OWIN owin是web services和framework组件之间的抽象.抽象包括两个核心要素: environment dictionary 这个数据结构存储处理HTTP请求必须的状态和相关的 ...
- Server.HTMLEncode用法
Server.HTMLEncode用法!! Server.HTMLEncode HTMLEncode 一.HTMLEncode 方法对指定的字符串应用 HTML 编码. 语法 Server.HTMLE ...
- C#图像处理(2):给图片加白边
C#图片处理给图片添加白边: /// <summary> /// 在图片上方加入白边 /// </summary> /// <param name="Img&q ...
- redis的5种数据结构的简介
5种数据结构 1.字符串 Redis 字符串是一个字节序列.在 Redis 中字符串是二进制安全的,这意味着它们没有任何特殊终端字符来确定长度,所以可以存储任何长度为 512 兆的字符串. 示例 12 ...
- 深入javascript——构造函数和原型对象
常用的几种对象创建模式 使用new关键字创建 最基础的对象创建方式,无非就是和其他多数语言一样说的一样:没对象,你new一个呀! var gf = new Object(); gf.name = &q ...