[英]Access $refs from other components not in the same level as current component
[英]Access other component refs from within component
我正在尋找一種使兩個組件能夠相互通信的方法。 我只是想這樣,當<InputCheckboxAll/>
被選中或取消選中它會選中或取消選中所有<InputCheckbox/>
組件。
var InputCheckboxAll = React.createClass({
render: function () {
return (
<input type='checkbox' {...this.props}/>
)
}
})
var InputCheckbox = React.createClass({
render: function () {
return (
<input type='checkbox' {...this.props}/>
)
}
})
<InputCheckboxAll ref='masterCheckbox'/>
<InputCheckbox masterRef='masterCheckbox'/>
<InputCheckbox masterRef='masterCheckbox'/>
<InputCheckbox masterRef='masterCheckbox'/>
如何從內<InputCheckboxAll>
部分我可以選擇與所有參masterCheckbox
的DOM?
將處理程序傳遞給InputCheckboxAll並將狀態傳遞給InputCheckbox。
var InputCheckboxAll = React.createClass({
handleChange: function(event) {
this.props.handleChange(event);
},
render: function () {
return (
<input type='checkbox' {...this.props} onChange={this.handleChange} />
)
}
})
var InputCheckbox = React.createClass({
render: function () {
var checkedValue = this.props.allChecked ? true : this.state.checked;
return (
<input checked={checkedValue} type='checkbox' {...this.props}/>
)
}
})
var CheckMaster = React.createClass({
getInitialState: function() { return {allChecked: false}; },
handleChange: function(event) {
this.setState({allChecked: event.target.value});
},
render: function () {
return (
<div>
<InputCheckboxAll handleChange={this.handleChange}/>
<InputCheckbox allChecked={this.state.allChecked}/>
<InputCheckbox allChecked={this.state.allChecked}/>
<InputCheckbox allChecked={this.state.allChecked}/>
</div>
)
}
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.