【发布时间】:2017-07-05 04:10:52
【问题描述】:
您好,我是 react 新手,我对 react 中的复选框单击处理有疑问。我想在选中复选框时显示 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}
)
}
}
【问题讨论】: