[React] React Fundamentals: Component Lifecycle - Updating
The React component lifecycle will allow you to update your components at runtime. This lesson will explore how to do that.
Updating: componentWillReceiveProps
componentWillReceiveProps(object nextProps)
Invoked when a component is receiving new props. This method is not called for the initial render.
Use this as an opportunity to react to a prop transition before render() is called by updating the state using this.setState(). The old props can be accessed via this.props. Callingthis.setState() within this function will not trigger an additional render.
Updating: shouldComponentUpdate
boolean shouldComponentUpdate(object nextProps, object nextState)
Invoked before rendering when new props or state are being received. This method is not called for the initial render or when forceUpdate is used.
Use this as an opportunity to return false when you're certain that the transition to the new props and state will not require a component update.
If shouldComponentUpdate returns false, then render() will be completely skipped until the next state change. (In addition, componentWillUpdate and componentDidUpdate will not be called.)
By default, shouldComponentUpdate always returns true to prevent subtle bugs when stateis mutated in place, but if you are careful to always treat state as immutable and to read only from props and state in render() then you can override shouldComponentUpdate with an implementation that compares the old props and state to their replacements.
If performance is a bottleneck, especially with dozens or hundreds of components, useshouldComponentUpdate to speed up your app.
Updating: componentDidUpdate
componentDidUpdate(object prevProps, object prevState)
Invoked immediately after the component's updates are flushed to the DOM. This method is not called for the initial render.
Use this as an opportunity to operate on the DOM when the component has been updated.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>React Lesson 11: Component Lifecycle: Updating</title>
<script src="http://fb.me/react-0.8.0.js"></script>
<script src="http://fb.me/JSXTransformer-0.8.0.js"></script>
<style>
body {
margin: 25px;
}
</style>
</head>
<body>
<div id="panel"></div>
<script type="text/jsx">
/** @jsx React.DOM */
var APP =
React.createClass({
getInitialState:function(){
return {increasing:false}
},
update:function(){
var newVal = this.props.val+1
this.setProps({val:newVal})
},
componentWillReceiveProps:function(nextProps){
//Invoked when a component is receiving new props. This method is not called for the initial render.
//So when the counter increase, this method will be called
//works for props, not state
this.setState({increasing:nextProps.val>this.props.val})
},
shouldComponentUpdate: function( nextProps, nextState){
/*Invoked before rendering when new props or state are being received. This method is not called for the initial render or when forceUpdate is used. Use this as an opportunity to return false when you're certain that the transition to the new props and state will not require a component update.*/
console.log(nextProps.val);
//Only update every 5 times
return nextProps.val % 5 ===0;
},
render:function(){
console.log(this.state.increasing)
return (
<button
onClick={this.update}>
{this.props.val}
</button>
)
},
componentDidUpdate: function( prevProps, prevState){
console.log("prevProps ===" + JSON.stringify(prevProps));
}
}); React.renderComponent(
<APP val={0} />,
document.getElementById('panel')) </script>
</body>
</html>
https://facebook.github.io/react/docs/component-specs.html
[React] React Fundamentals: Component Lifecycle - Updating的更多相关文章
- [React Fundamentals] Component Lifecycle - Updating
The React component lifecycle will allow you to update your components at runtime. This lesson will ...
- [React Fundamentals] Component Lifecycle - Mounting Usage
The previous lesson introduced the React component lifecycle mounting and unmounting. In this lesson ...
- [React Fundamentals] Component Lifecycle - Mounting Basics
React components have a lifecycle, and you are able to access specific phases of that lifecycle. Thi ...
- [React ] React Fundamentals: Component Lifecycle - Mounting Usage
The previous lesson introduced the React component lifecycle mounting and unmounting. In this lesson ...
- [React] React Fundamentals: Component Lifecycle - Mounting Basics
React components have a lifecycle, and you are able to access specific phases of that lifecycle. Thi ...
- React.js Tutorial: React Component Lifecycle
Introduction about React component lifecycle. 1 Lifecycle A React component in browser can be any of ...
- React.createClass 、React.createElement、Component
react里面有几个需要区别开的函数 React.createClass .React.createElement.Component 首选看一下在浏览器的下面写法: <div id=" ...
- React 中的 Component、PureComponent、无状态组件 之间的比较
React 中的 Component.PureComponent.无状态组件之间的比较 table th:first-of-type { width: 150px; } 组件类型 说明 React.c ...
- React Native 中 component 生命周期
React Native 中 component 生命周期 转自 csdn 子墨博客 http://blog.csdn.net/ElinaVampire/article/details/518136 ...
随机推荐
- [cocos2dx 3.x]Label类数字变化动作
之前写了个2.14版本的动作变化,见 http://www.cnblogs.com/creeper/p/3531304.html 3.x版本变化了很多,但是核心思想还是没有变化,所以对应3.x版本的改 ...
- 用PersonalRank实现基于图的推荐算法
今天我们讲一个下怎么使用随机游走算法PersonalRank实现基于图的推荐. 在推荐系统中,用户行为数据可以表示成图的形式,具体来说是二部图.用户的行为数据集由一个个(u,i)二元组组成,表示为用户 ...
- python的whl文件安装
python27win32安装:https://www.python.org/downloads/ 下载2.7的 安装中一路下一步即可: 配置python系统环境变量: 新建: PY ...
- ODBC具体使用
应用程序 应用程序对外提供使用者交谈界面,同时对内执行资料之准备工作数据库系统所传回来的结果在显示给使用者看.简单来说,应用程序即ODBC 界面执行下列主要工作:1. Request a connec ...
- Oracle EBS R12 WIP Component Issue&Return Process
oracleassemblytransactionscomponentsjobsreference 目录(?)[-] 定义BOM 定义Routing 定义WIP Discrete Job 发料 Mat ...
- App运营者必须知道的30款数据分析工具
如今的移动应用早已不再是某种结构单一.功能简单的工具了.当我们的移动应用变得越来越庞杂,我们便会需要借用分析工具,来跟踪和分析App内的每一个部分.幸运的是,目前市面上有许多数据分析工具可供App开发 ...
- Oracle rman 命令详解
一.list常用命令总结备忘 list命令列出控制文件.RMAN恢复目录中备份信息, 是我们对所有可见的数据库备份文件的一个最直观的了解的方法 list incarnation; list ba ...
- 虚拟主机apache
1.虚拟主机配置 windows: 1)加载配置虚拟主机的配置文件,在Apache/conf中找到httpd.conf文件,并搜索出以下的两句话,将Include conf/extra/httpd-v ...
- css滑动门制作圆角按钮
之前做项目的时候,基本都是将圆角背景图切成三块,故而每次用的标签都超级多,a标签中总是包含三个span,然后里面还得放按钮,导航冗余标签极多. 事实上是之前理解的滑动门的精髓不够到位. 现在有两种方式 ...
- 如何判断 Android 应用的 Apk 签名是否一致?
可以比对apk签名的fingerprint. 假定安装了JDK,如果想查HelloWorld.apk所使用的签名的fingerprint,可以这样做: 1. 查找apk里的rsa文件 (Windows ...