[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 ...
随机推荐
- swift调用相机和相册
简单实现swift调用相机和相册的功能,分享代码与学习swift的童鞋共同进步 import UIKit class ViewController: UIViewController,UIImageP ...
- 如何单独编译Android源代码中的模块
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6566662 第一次下载好Android源代码工 ...
- Android 通知栏用法例子
当程序意外退出时,可以去掉通知栏上显示的图标 1.创建TestNotificationActivity activity类, package com.notioni.test.notification ...
- Git diff (---和+++具体解释)
如果两个文件相似度很高,那么上下文格式的diff,将显示大量重复的内容,很浪费空间.1990年,GNU diff率先推出了"合并格式"的diff,将f1和f2的上下文合并在一起显示 ...
- 伸缩放大的js
window.onload = function() { var div1 = document.getElementById('div1'); div1.onmous ...
- 定义block块
一: 工程图 二: 代码区 AppDelegate.h #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <U ...
- UVA10305 拓扑排序
网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=117863#problem/B 思路分析:裸的拓扑排序,注释在代码中. 代码: #i ...
- Trident内核中取验证码图片的几种方法
程序中用了IE的内核,想取出网站中的验证码图片,单独显示出来,调研了以下几路方法 1.枚举所有缓存文件,进行处理,找到想要的,核心代码 )//这段代码可以枚举所有缓存资源,然后对应做处理 { LPIN ...
- 遍历json的方式
var obj = eval("(" + data + ")"); for(var key in obj) { alert(obj[key]); }
- 修改mysql错误提示语言的方法
百度真的太恶心太无能了. 装了个mysql5,结果发现错误提示语言是法语. 然后我就想改成把错误提示语改成英语. 然后我使用各种关键字和方法百度了一个上午,居然全TM是告诉我怎么处理mysql中文乱码 ...