你好,我是新来的反应,我有一个关于复选框点击处理反应的问题。我想在选中复选框时显示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
很高兴,帮助你:) –