props與state,前者類似于函數(shù)調(diào)用的參數(shù),后者類似于函數(shù)內(nèi)部的局部變量props,組件外部也無(wú)法修改state,props表示那些一旦定義,就不再改變的特性,而state是會(huì)隨著用戶互動(dòng)而產(chǎn)生變化的特性http://wiki.jikexueyuan.com/project/notes/images/react_component_lifecycle.png" alt="react_component_lifecycle.png" />
this.props.children與React.Children,前者包括所有的屬性,后者是react的一個(gè)API,處理了不同的情形,使得需要使用前者時(shí)不用考慮各種情況propTypes來(lái)限定屬性的類型,使用getDefaultProps來(lái)設(shè)置屬性的默認(rèn)值,例如:var MyTitle = React.createClass({
propTypes: {
title: React.PropTypes.string.isRequired,
},
getDefaultProps : function () {
return {
title : 'Hello World'
};
},
render: function() {
return <h1> {this.props.title} </h1>;
}
});
ref屬性,獲取真實(shí)DOM節(jié)點(diǎn),必須等到虛擬 DOM 插入文檔以后,才能使用這個(gè)屬性:var MyComponent = React.createClass({
handleClick: function() {
this.refs.myTextInput.focus();
},
render: function() {
return (
<div>
<input type="text" ref="myTextInput" />
<input type="button" value="Focus the text input" onClick={this.handleClick} />
</div>
);
}
});
this.props讀取,可以通過(guò)設(shè)置事件回調(diào)響應(yīng)輸入變化