React Native之ReactJs生命周期(四)

前端之家收集整理的这篇文章主要介绍了React Native之ReactJs生命周期(四)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
1.创建阶段
getDefaultProps:处理props的默认值 在React.createClass调用
2.实例化阶段
getInitialState、componentWillMount、render、componentDidMount
state:组件的属性,主要是用来存储组件资深需要的数据,每次数据的更新都是通过
修改state属性的值,ReactJs内部会监听state属性的变化,一旦发生变化的话,就会主动
触发组件的render方法来更新虚拟DOM结构
虚拟DOM:将真实的DOM结构映射成一个JSON数据结构
3.更新阶段
主要发生在用户操作之后或父组件有更新的时候,此时会根据用户的操作行为进行相应的页面结构的调整
componentWillReceiveProps、shouldComponentUpdate、componentWillUpdate、render
4.销毁阶段
销毁时调用,通常做一些取消事件绑定、移除虚拟DOM中对应的组件数据结构、销毁一些无效的定时器等

componentWillUnmount

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head lang="en">
  4. <Meta charset="UTF-8"/>
  5. <title>Hello World</title>
  6. <script src="react.js"></script>
  7. <script src="react-dom.js"></script>
  8. <script type="text/javascript" src="browser.min.js"></script>
  9. </head>
  10. <body>
  11. <div id="root"></div>
  12. <script type="text/babel">
  13. var ShowEditor=React.createClass(
  14. {
  15. //1.创建阶段
  16. getDefaultProps:function () {
  17. //在创建类的时候调用,this.props该组件的默认属性
  18. console.log("getDefaultProps=="+this.props);
  19. },//2.实例化阶段
  20. getInitialState:function () {
  21. //初始化组件的state的值,其返回值会赋给组件的this.state熟悉
  22. console.log("getInitialState=="+this.state);
  23. return {value:'请输入文字'};
  24.  
  25. },componentWillMount:function () {
  26. //在render之前调用
  27. //业务逻辑的处理都应该放在这里,如对state的操作
  28. this.state.value="componentWillMount";
  29. console.log("componentWillMount=="+this.state.value);
  30. },handleChange:function (event) {
  31.  
  32. this.setState({value: event.target.value});
  33. //alert("时间:"+event.target.value);
  34. },render:function () {
  35. //渲染返回一个DOM
  36. console.log("render");
  37. return(
  38. <div>
  39. <h3>输入</h3>
  40. <textarea style={{width:300,height:200,outline:'none'}}
  41. onChange={this.handleChange}
  42. ref="textarea"
  43. defaultValue={this.state.value}
  44. />
  45. <h3>输出</h3>
  46. <div>{this.state.value}</div>
  47. </div>
  48. );
  49. },componentDidMount:function () {
  50. //在render之后调用
  51. //在该方法中,ReactJS会使用render方法返回的虚拟DOM 对象来创建真是的DOM结构
  52. // 在组件内部可以通过this.getDOMNode()来获取当前组件的节点
  53. console.log("componentDidMount");
  54. this.state.value="componentDidMount";
  55.  
  56. }//3.更新阶段 主要发生在用户操作之后或者父组件有更新的时候,此时会根据用户的操作行为进行相应的页面结构调整,componentWillReceiveProps:function () {
  57. //当this.props被调用时或者父组件调用setProps()之后调用
  58. console.log("componentWillReciverProps");
  59. },shouldComponentUpdate:function () {
  60. //用来拦截新的props或state,根据逻辑来判断
  61. //是否需要更新
  62. console.log("shouldComponentUpdate");
  63. return true;
  64. },componentWillUpdate:function () {
  65. //shouldComponentUpdate返回true执行
  66. // 组件将更新
  67. console.log("componentWillUpdate");
  68. },componentDidUpdate:function () {
  69. //组件更新完毕调用
  70. console.log("componentDidUpdate");
  71. }
  72. //销毁阶段,componentWillUnmount:function () {
  73. //销毁时调用,通常做一些取消事件绑定、移除虚拟DOM中对应的组件数据结构、销毁一些无效的定时器等
  74. console.log("componentWillUnmount");
  75. }
  76. }
  77. );
  78. ReactDOM.render(<ShowEditor />,document.getElementById("root"));
  79. </script>
  80. </body>
  81. </html>

猜你在找的React相关文章