執行個體:查看觸發順序。
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>測試</title></head><body> <script type="text/javascript" src="http://cdn.bootcss.com/react/0.13.2/react.min.js"></script> <script type="text/javascript" src="http://cdn.bootcss.com/react/0.13.2/JSXTransformer.js"></script> <script type="text/jsx"> var style={ color:"red", border:"1px solid #f99", width:"200px", height:"50px" }; var HelloWorld=React.createClass({ getDefaultProps: function(){ console.log("getDefaultProps,1") }, getInitialState: function(){ console.log("getInitialState,2"); return null; }, componentWillMount: function(){ console.log("componentWillMount,3") }, render: function(){ console.log("render,4") return <p ref="childp">hello,{( function(obj){ if(obj.props.name) return obj.props.name else return "world" } )(this)}</p> }, componentDidMount:function(){ console.log("componentDidMount,5"); }, }); React.render(<div style={style}>HelloWorld</div>,document.body) </script></body></html>
注意上面代碼中紅色的標記部分,我們只是輸出的字串HelloWorld,並不是標籤<HelloWorld></Helloworld>,所以此時的控制台和輸出是這樣。
我們可以看出,getDefaultProps在實際的使用中,是直接調用的,也就是在React.createClass之後,就會被調用並把結果儲存起來,及時你沒有產生執行個體,這個函數也會被調用,react這麼做主要目的就是為了提高效能,儘可能早的將我們要做的事情處理好,這樣當我們要使用HelloWorld執行個體的時候,就會省掉調用這個函數的時間從而提高效能。我們改一下代碼,讓其正確輸出。
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>測試</title></head><body> <script type="text/javascript" src="http://cdn.bootcss.com/react/0.13.2/react.min.js"></script> <script type="text/javascript" src="http://cdn.bootcss.com/react/0.13.2/JSXTransformer.js"></script> <script type="text/jsx"> var style={ color:"red", border:"1px solid #f99", width:"200px", height:"50px" }; var HelloWorld=React.createClass({ getDefaultProps: function(){ console.log("getDefaultProps,1") }, getInitialState: function(){ console.log("getInitialState,2"); return null; }, componentWillMount: function(){ console.log("componentWillMount,3") }, render: function(){ console.log("render,4") return <p ref="childp">hello,{( function(obj){ if(obj.props.name) return obj.props.name else return "world" } )(this)}</p> }, componentDidMount:function(){ console.log("componentDidMount,5"); }, }); React.render(<div style={style}><HelloWorld></HelloWorld></div>,document.body) </script></body></html>
各個執行個體的正確用法
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>測試</title></head><body> <script type="text/javascript" src="http://cdn.bootcss.com/jquery/2.0.3/jquery.min.js"></script> <script type="text/javascript" src="http://cdn.bootcss.com/react/0.13.2/react.min.js"></script> <script type="text/javascript" src="http://cdn.bootcss.com/react/0.13.2/JSXTransformer.js"></script> <script type="text/jsx"> $(document).ready( function(){ var count=0; var style={ color:"red", border:"1px solid #090", }; var HelloWorld=React.createClass({ getDefaultProps:function(){ console.log("getDefaultProps,1"); return{name:"Tom"}; }, getInitialState:function(){ console.log("getInitialState,2"); return{ myCount:count++, ready:false }; }, componentWillMount:function(){ console.log("componentWillMount,3"); this.setState({ready:true}); }, render:function(){ console.log("render,4"); return <p ref="childp">Hello,{ this.props.name ? this.props.name : "World" }<br/>{""+this.state.ready}</p>; }, componentDidMount:function(){ console.log("componentDidMount,5"); //這裡才可以操作dom $(React.findDOMNode(this)).append("surprise!"); }, //HelloWolrld內部 }); React.render(<div style={style}><HelloWorld></HelloWorld></div>,document.body) //function 內部 } //ready內部 ) </script></body></html>
輸出count,產生多個HelloWorld
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>測試</title></head><body> <script type="text/javascript" src="http://cdn.bootcss.com/jquery/2.0.3/jquery.min.js"></script> <script type="text/javascript" src="http://cdn.bootcss.com/react/0.13.2/react.min.js"></script> <script type="text/javascript" src="http://cdn.bootcss.com/react/0.13.2/JSXTransformer.js"></script> <script type="text/jsx"> $(document).ready( function(){ var count=0; var style={ color:"red", border:"1px solid #090", }; var HelloWorld=React.createClass({ getDefaultProps:function(){ console.log("getDefaultProps,1"); return{name:"Tom"}; }, getInitialState:function(){ console.log("getInitialState,2"); return{ myCount:count++, ready:false }; }, componentWillMount:function(){ console.log("componentWillMount,3"); this.setState({ready:true}); }, render:function(){ console.log("render,4"); return <p ref="childp">Hello,{ this.props.name ? this.props.name : "World" }<br/>{""+this.state.ready}{this.state.myCount}</p>; }, componentDidMount:function(){ console.log("componentDidMount,5"); $(React.findDOMNode(this)).append("surprise!"); }, //HelloWolrld內部 }); React.render(<div style={style}><HelloWorld></HelloWorld><br/><HelloWorld></HelloWorld></div> ,document.body) //function 內部 } //ready內部 ) </script></body></html>