1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < title > React-Component-生命周期</ title >
5+ < meta charset ="UTF-8 ">
6+ < meta name ="viewport " content ="width=device-width, initial-scale=1 ">
7+ <!--React 核心库-->
8+ < script src ="../../../js/react.js "> </ script >
9+ <!--React 跟 Dom 相关的功能库-->
10+ < script src ="../../../js/react-dom.js "> </ script >
11+ <!--jsx 转换 js 的框架 babel-->
12+ < script src ="../../../js/browser.min.js "> </ script >
13+ </ head >
14+ < body >
15+ < fieldset >
16+ < legend > < h3 > componentWillMount</ h3 > </ legend >
17+ < div id ="div1 "> </ div >
18+ </ fieldset >
19+
20+ < script type ="text/babel ">
21+ var Button = React . createClass ( {
22+ getInitialState : function ( ) {
23+ return {
24+ data :0
25+ } ;
26+ } ,
27+ setNewNumber : function ( ) {
28+ //reset state
29+ //当 state 发生改变的时候,state 对应的组件会重新挂载
30+ //将 state 绑定到对应的组件
31+ this . setState ( { data : this . state . data + 1 } )
32+ } ,
33+ render : function ( ) {
34+ var content ;
35+ if ( this . state . data % 2 == 0 ) {
36+ content = < Content myNumber = { this . state . data } > </ Content > ;
37+ } else {
38+ content = < h3 > { this . state . data } </ h3 > ;
39+ }
40+ return (
41+ < div >
42+ < button onClick = { this . setNewNumber } > INCREMENT</ button >
43+ { content }
44+ </ div >
45+ ) ;
46+ }
47+ } )
48+
49+
50+ var Content = React . createClass ( {
51+ getInitialState : function ( ) {
52+ console . log ( 'state' ) ;
53+ return {
54+ } ;
55+ } ,
56+ getDefaultProps : function ( ) {
57+ console . log ( 'props' ) ;
58+ return { } ;
59+ } ,
60+ componentWillMount :function ( ) {
61+ console . log ( 'Component WILL MOUNT!' )
62+ } ,
63+ componentDidMount :function ( ) {
64+ console . log ( 'Component DID MOUNT!' )
65+ } ,
66+ componentWillReceiveProps :function ( newProps ) {
67+ console . log ( 'Component WILL RECEIVE PROPS!' )
68+ } ,
69+ shouldComponentUpdate :function ( newProps , newState ) {
70+ return true ;
71+ } ,
72+ componentWillUpdate :function ( nextProps , nextState ) {
73+ console . log ( '准备更新组件' ) ;
74+ } ,
75+ componentDidUpdate :function ( prevProps , prevState ) {
76+ console . log ( '组件已经更新' )
77+ } ,
78+ componentWillUnmount :function ( ) {
79+ console . log ( 'Component WILL UNMOUNT!' )
80+ } ,
81+ render : function ( ) {
82+ console . log ( 'render' ) ;
83+ return (
84+ < h3 > { this . props . myNumber } </ h3 >
85+ ) ;
86+ }
87+ } ) ;
88+
89+ ReactDOM . render ( < Button /> , document . getElementById ( 'div1' ) ) ;
90+ </ script >
91+ </ body >
92+ </ html >
0 commit comments