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.

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>React Lesson 11: Component Lifecycle: Updating</title>
  6. <script src="http://fb.me/react-0.8.0.js"></script>
  7. <script src="http://fb.me/JSXTransformer-0.8.0.js"></script>
  8. <style>
  9. body {
  10. margin: 25px;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <div id="panel"></div>
  16. <script type="text/jsx">
  17. /** @jsx React.DOM */
  18. var APP =
  19. React.createClass({
  20. getInitialState:function(){
  21. return {increasing:false}
  22. },
  23. update:function(){
  24. var newVal = this.props.val+1
  25. this.setProps({val:newVal})
  26. },
  27. componentWillReceiveProps:function(nextProps){
  28. //Invoked when a component is receiving new props. This method is not called for the initial render.
  29. //So when the counter increase, this method will be called
  30. //works for props, not state
  31. this.setState({increasing:nextProps.val>this.props.val})
  32. },
  33. shouldComponentUpdate: function( nextProps, nextState){
  34. /*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.
  35.  
  36. 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.*/
  37. console.log(nextProps.val);
  38. //Only update every 5 times
  39. return nextProps.val % 5 ===0;
  40. },
  41. render:function(){
  42. console.log(this.state.increasing)
  43. return (
  44. <button
  45. onClick={this.update}>
  46. {this.props.val}
  47. </button>
  48. )
  49. },
  50. componentDidUpdate: function( prevProps, prevState){
  51. console.log("prevProps ===" + JSON.stringify(prevProps));
  52. }
  53. });
  54.  
  55. React.renderComponent(
  56. <APP val={0} />,
  57. document.getElementById('panel'))
  58.  
  59. </script>
  60. </body>
  61. </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. C语言考试解答十题

    学院比较奇葩,大一下期让学的VB,这学期就要学C++了,然后在开学的前三个周没有课,就由老师讲三个周的C语言,每天9:30~11:30听课,除去放假和双休日,实际听课时间一共是12天*2小时,下午是1 ...

  2. 弱安全协议探测工具-sslciphercheck

    SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为 网络通信提供安全及数据完整性的一种安全协议.TLS ...

  3. Dagger 2: Step To Step

    文/iamwent(简书作者)原文链接:http://www.jianshu.com/p/7505d92d7748著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 假设你已经了解 依赖注 ...

  4. BrandZ:2016年全球最具价值品牌百强榜(完整报告)

    https://wppbaz.com/admin/uploads/files/BZ_Global_2016_Report.pdf Millward Brown编制的BrandZ最新排行榜(2016 B ...

  5. BASE64编码规则及C#实现

    一.编码规则      Base64编码的思想是是采用64个基本的ASCII码字符对数据进行重新编码.它将需要编码的数据拆分成字节数组.以3个字节为一组.按顺序排列24位数据,再把这24位数据分成4组 ...

  6. Win系统下制作U盘CLOVER引导+安装原版Mavericks10.9

    啃苹果有一段时间了,之前一直用白苹果,但是白苹果配置有所限制,对于我搞音频的人来讲,显得有点拖沓.所以研究了将近2年的黑苹果,最近心血来潮给大家一个比较傻瓜式的教程,首先强调一点,黑苹果是需要折腾的, ...

  7. JDK/bin目录下的不同exe文件的用途(转)

    新安装完JDk 大家是否发现安装目录的bin文件夹有很多exe文件 下面就为大家讲解不同exe文件的用途 javac:Java编译器,将Java源代码换成字节代 java:Java解释器,直接从类文件 ...

  8. sql - and - or

    sql - and SQL AND links together two or more conditional statements for increased filtering when run ...

  9. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.1.3

    Use the QR decomposition to prove Hadamard's inequality: if $X=(x_1,\cdots,x_n)$, then $$\bex |\det ...

  10. 从微信推送看Android Service的创建和销毁

    启动服务是有两组参数影响服务的状态. 1.在onStartCommand(Intent intent, int flags, int startId) 接口中返回值,例如 START_STICKY;  ...