你好,我是新來的反應,我有一個關於複選框點擊處理反應的問題。我想在選中複選框時顯示div,如果取消選中複選框,請移除div。顯示覆選框中的組件點擊反應
我這樣做的方式只顯示單擊複選框時的div,但未選中時不會刪除div。我如何在反應中做到這一點?
class QuestionOverlay extends Component {
constructor() {
super();
this.showComments = this.showComments.bind(this);
this.state = {
showComponent: false,
};
}
showComments = (e) => {
this.setState({
showComponent: true,
});
}
render() {
return (
<div className="add_checkbox">
<span>Enable Comments</span>
<input className="checkbox" type="checkbox" name="enable_comment" onClick={this.showComments} value="enable_comment"/>
</div>
{this.state.showComponent ? <div className="comments_preview_sample"></div> : null}
)
}
}
正是我所需要的感謝! – CraZyDroiD
很高興,幫助你:) –