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的更多相关文章

  1. [React Fundamentals] Component Lifecycle - Updating

    The React component lifecycle will allow you to update your components at runtime. This lesson will ...

  2. [React Fundamentals] Component Lifecycle - Mounting Usage

    The previous lesson introduced the React component lifecycle mounting and unmounting. In this lesson ...

  3. [React Fundamentals] Component Lifecycle - Mounting Basics

    React components have a lifecycle, and you are able to access specific phases of that lifecycle. Thi ...

  4. [React ] React Fundamentals: Component Lifecycle - Mounting Usage

    The previous lesson introduced the React component lifecycle mounting and unmounting. In this lesson ...

  5. [React] React Fundamentals: Component Lifecycle - Mounting Basics

    React components have a lifecycle, and you are able to access specific phases of that lifecycle. Thi ...

  6. React.js Tutorial: React Component Lifecycle

    Introduction about React component lifecycle. 1 Lifecycle A React component in browser can be any of ...

  7. React.createClass 、React.createElement、Component

    react里面有几个需要区别开的函数 React.createClass .React.createElement.Component 首选看一下在浏览器的下面写法: <div id=" ...

  8. React 中的 Component、PureComponent、无状态组件 之间的比较

    React 中的 Component.PureComponent.无状态组件之间的比较 table th:first-of-type { width: 150px; } 组件类型 说明 React.c ...

  9. React Native 中 component 生命周期

    React Native 中 component 生命周期 转自 csdn 子墨博客  http://blog.csdn.net/ElinaVampire/article/details/518136 ...

随机推荐

  1. java客户端连接MongoDB数据库的简单使用

    1.下载mongoDB的jar包,并引入到工程的CLASSPATH中下载:mongodb2.5驱动包下载 如果使用maven项目,最新的依赖如下: <dependency> <gro ...

  2. C++的表驱动法

    目的:使用表驱动法,替换复杂的if/else和switch/case语句. 说明:JS 等其他语言也都支持的. 表驱动发示例:http://blog.csdn.net/zhouyulu/article ...

  3. 157. Read N Characters Given Read4

    题目: The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the ...

  4. JS 中Promise 模式

    异步模式在web编程中变得越来越重要,对于web主流语言Javscript来说,这种模式实现起来不是很利索,为此,许多Javascript库(比如 jQuery和Dojo)添加了一种称为promise ...

  5. jni.h源码

    /* * jni.h * Java Native Interface. * * Copyright (c) 1996, 1997 * Transvirtual Technologies, Inc. A ...

  6. 结构体dict_table_t

    typedef struct dict_table_struct dict_table_t; /** Data structure for a database table. Most fields ...

  7. C#常用的命名规范

    C#常用的命名规则 Pascal 规则 每个单词开头的字母大写(如 TestCounter). Camel 规则 除了第一个单词外的其他单词的开头字母大写. 如. testCounter. Upper ...

  8. Memcached‘process_bin_delete’函数安全漏洞

    漏洞名称: Memcached‘process_bin_delete’函数安全漏洞 CNNVD编号: CNNVD-201401-174 发布时间: 2014-01-15 更新时间: 2014-01-1 ...

  9. MFC CVIew关闭时崩溃

    记得看视频的时候老师说过    创建CView的时候,也就是创建视图的时候,不要使用  Cview      m_view;这种方式 而是使用Cview  *  pView=new   Cview() ...

  10. Ubuntu 12.04 和 Win7 双系统安装

    Thinkpad T400上成功安装双系统 安装Win7 使用光盘按步骤安装,到这里是一个没有分区的硬盘,做了如下分区: 100M(系统保留),40G(C盘),60G(D盘),80G(E盘),52G( ...