我需要初始化父組件的狀態。所以我用下面的方法來做:通過道具初始化子組件的狀態是否正確?
var Timer = React.createClass({
getInitialState: function() {
return {timer: this.props.timer};
},
render() {
return <div>{this.state.timer}</div>
}
});
var App = React.createClass({
getInitialState: function() {
return {timer: 1000};
},
render() {
return <Timer timer={this.state.timer}>
}
});
用這種方式初始化Timer組件的狀態是否正確?