2
모델 변경 이벤트를 처리하는 올바른 방법입니까?React.js - 이벤트 처리기로 소유자 구성 요소의 상태 변경
a. handleModelChange 함수는 onModelChange prop를 SubClass로 전달 중입니다.
b. 모델 변경 이벤트가 발생하면 다시 렌더링하기 위해 SubComponent의 핸들러가 MainComponent의 상태를 변경합니다.
var _SomeMixin={
componentWillMount: function() {
this.props.options.model.on("MODEL_CHANGED", this.props.onModelChange);
},
componentWillUnmount: function() {
this.props.options.model.off("MODEL_CHANGED", this.props.onModelChange);
},
/* more mixin functions */
}
var SubComponent = React.createClass({
mixins: [_SomeMixin],
render: function() {
return (
<div>
<!-- ... more elements .. >
</div>
);
}
});
var MainComponent = React.createClass({
getInitialState: function() {
return {MainComponentState: []};
},
handleModelChange: function() {
if (this.isMounted()) {
this.setState({MainClassState: this.props.options.model.toJSON()});
}
},
render: function() {
return (
<SubClass options={this.props.options} onModelChange={this.handleModelChange} />
);
}
});