简体   繁体   English

在复选框上显示组件单击反应

[英]Showing components on checkbox click in react

Hello i'm new to react and I have a problem regarding checkbox click handling in react. 您好,我是新来的响应者,我对react中的复选框单击处理有疑问。 I want to show a div when a check box is checked and remove the div if the checkbox is unchecked. 我想在选中复选框时显示div,如果未选中该复选框,则删除div。

The way I have done this only shows the div when the checkbox is clicked but it does not remove the div when it is unchecked. 我这样做的方式仅在单击复选框时显示div,但在未选中时不会删除div。 How can I do this in react? 我该如何反应?

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}

        )
    }
}

Reason is you are always setting the value of showComponent=true , you need to reset the state variable when the checkbox is unchecked, Use this: 原因是您始终在设置showComponent=true的值, showComponent=true需要在取消选中复选框时重置状态变量,请使用以下命令:

showComments(e){

    this.setState({
        showComponent: e.target.checked,
    });

}

Check the working fiddle: https://jsfiddle.net/mrqutgbz/ 检查工作提琴: https : //jsfiddle.net/mrqutgbz/

Few things you need to change: 您需要更改的几件事:

*You are returning 2 element from render , one div and one more div from conditional rendering . *您在returning从2元render ,一个div还有一个div ,从有条件的rendering We can't return more than one html element from render , so put the conditional rendering in the main div . 我们不能从render返回多个html元素,因此将条件渲染放入主div

*You are binding the showComments method twice, one in constructor and another by using arrow , remove the arrow , that is not required. *您要binding showComments方法两次,一次在constructor ,另一次通过使用arrow ,删除arrow ,这不是必需的。

*Div that you are rendering on condition is empty, put some content in that. *如果您要rendering的条件为空,请在其中添加一些内容。

You need to change your onClick listener to onChange . 您需要将onClick侦听器更改为onChange Then, rename showComments to toggleComments and implement it like so: 然后,将showComments重命名为toggleComments并实现它,如下所示:

toggleComments(e) {
  this.setState({ showComponent: e.target.checked });
}

Here are several syntax error in your code: 这是代码中的一些语法错误:

  1. Function defined in a class can't use = way. class定义的函数不能使用= way。
  2. React render function need a root container like div tag. React render函数需要一个像div标签这样的根容器。

 const { Component } = React; const { render } = ReactDOM; class QuestionOverlay extends Component { constructor(props) { super(props); this.state = { showComponent: false } this.showComments = this.showComments.bind(this); } showComments() { this.setState({ showComponent: !this.state.showComponent }); } render() { return ( <div> <div className="add_checkbox"> Enable Comments <br/> <input type="checkbox" onClick={this.showComments} /> </div> {this.state.showComponent ? <div className="comments_preview_sample">comments</div> : null} </div> ); } } render( <QuestionOverlay />, document.getElementById('root') ); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="root"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM