image.png
Animation30.gif
image.png

源码

  1. //生命周期脚本
  2. var HelloMassage = React.createClass({
  3. //设置默认属性
  4. getDefaultProps: function() {
  5. console.log("初始化默认的属性");
  6. return {
  7. name: "test"
  8. }
  9. },
  10. //设置默认状态
  11. getInitialState: function() {
  12. console.log("初始化状态成功");
  13. return {
  14. isLoading: false
  15. }
  16. },
  17. //组件被挂载之前,Render 之前最后一次修改状态的机会
  18. componentWillMount: function() {
  19. //修改状态
  20. this.setState({
  21. isLoading: true
  22. });
  23. console.log("组件被挂载之前");
  24. },
  25. //组件被渲染之后
  26. componentDidMount: function() {
  27. console.log("组件被真实的渲染到页面中完成");
  28. // console.log(this.refs);
  29. // console.log(this.refs.uname);
  30. //获取 jquery dom
  31. // console.log($(this.refs.uname));
  32. //获取 value
  33. // console.log($(this.refs.uname).val());
  34. //修改 css
  35. // $(this.refs.uname).css({
  36. // "color": "red"
  37. // });
  38. //自动完成(Autocomplete) 单词补充的示例
  39. // var availableTags = [
  40. // "ActionScript",
  41. // "AppleScript",
  42. // "Asp",
  43. // "BASIC",
  44. // "C",
  45. // "C++",
  46. // "Clojure",
  47. // "COBOL",
  48. // "ColdFusion",
  49. // "Erlang",
  50. // "Fortran",
  51. // "Groovy",
  52. // "Haskell",
  53. // "Java",
  54. // "JavaScript",
  55. // "Lisp",
  56. // "Perl",
  57. // "PHP",
  58. // "Python",
  59. // "Ruby",
  60. // "Scala",
  61. // "Scheme"
  62. // ];
  63. // $(this.refs.uname).autocomplete({
  64. // source: availableTags
  65. // });
  66. //获取 ReactDOM 这个获取到的是一个纯粹的 DOM 节点
  67. console.log(ReactDOM.findDOMNode(this.refs.uname));
  68. },
  69. //不能修改属性和状态
  70. componentWillUpdate: function(data) {
  71. console.log("componentWillUpdate", data);
  72. },
  73. //可以修改 DOM
  74. componentDidUpdate: function(data) {
  75. console.log("componentDidUpdate", data);
  76. },
  77. //返回 false 会阻止 render 调用
  78. shouldComponentUpdate: function(nextrops) {
  79. console.log("shouldComponentUpdate", nextrops);
  80. //返回一个 boolean 值
  81. // return false;//false 的话页面就不会更新
  82. return true;
  83. },
  84. //父组件修改属性触发,可以修改新属性和方法
  85. componentWillReceiveProps: function(nextrops) {
  86. console.log("componentWillReceiveProps", nextrops);
  87. },
  88. _change: function(e) {
  89. // console.log(e);
  90. // console.log(e.target);
  91. console.log(e.target.value);
  92. },
  93. render: function() {
  94. console.log("render 页面");
  95. return (
  96. <div>
  97. {/*这里可以获取到父组件传递过来的 name 属性的值*/}
  98. <label htmlFor="uname">{this.props.name}</label>
  99. {/*<input ref="uname" value={this.props.name} onChange={this._change} type="text"/>*/}
  100. <input id="uname" ref="uname" onChange={this._change} type="text"/>
  101. <h1>{this.props.name}</h1>
  102. {!this.state.isLoading ? <h1>loading...</h1> : <h1>ok</h1>}
  103. </div>
  104. )
  105. }
  106. });
  107. //新建一个父组件
  108. var HelloWorld = React.createClass({
  109. getInitialState: function() {
  110. return {
  111. data: "父容器的状态"
  112. }
  113. },
  114. click: function() {
  115. this.setState({
  116. data: "被改变"
  117. })
  118. },
  119. render: function() {
  120. return (
  121. <form onClick={this.click}>
  122. {/*使用父组件标签包裹着子组件 这样就可以通过父组件来给子组件传递一些属性了
  123. */}
  124. <HelloMassage name={this.state.data} />
  125. </form>
  126. )
  127. }
  128. });
  129. ReactDOM.render(
  130. <HelloWorld name="我是父组件" />,
  131. document.getElementById("example")
  132. );