简体   繁体   English

反应按钮onClick无法正常运行

[英]React button onClick not functioning properly

can you help me understand why this onClick event does not bind to the button and won't fire? 您能帮助我理解为什么此onClick事件未绑定到button并且不会触发吗?

class DateSlider extends React.Component{
    constructor(props){
        super(props);

        this.state = {
            allValues: this.props.allValues,
        }

        this.onClick = this.onClick.bind(this);
    }

    onClick(){
        console.log('here');
        // this.props.change(event.target.text);
    }

    render(){
        return (
            <div class="date-slider col-xl-4 col-12">
                <div class="row">
                    <div class="date-input-description col-xl-12 col-12">{this.props.unit}</div> 
                    <div class="col-xl-12 date-picker-container">
                        <div class="col-xl-12">
                            <div class="row" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                <div class="date-range col-xl-9 col-9">{this.props.initialValue}</div>
                                <div class="col-xl-3 col-9 date-picker-small-button">
                                    <img class="mx-auto d-block" src="./images/small-arrow-down.svg" />
                                </div>
                                <div class="dropdown-menu dropdown-menu-right">
                                    {
                                        //HERE
                                        this.state.allValues.map((value) => {
                                            return <button key={value} class="dropdown-item" type="button" onClick={this.onClick}>{value}</button>

                                        })
                                    }
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        );
    }

} 

When clicking on the item that is rendered, it won't console.log anything. 单击渲染的项目时,不会console.log任何内容。

 this.state.allValues.map((value) => {
    return <button key={value} class="dropdown-item" type="button" onClick={this.onClick}>{value}</button>})  

should be 应该

this.state.allValues.map((value) => {
  return <button key={value} className="dropdown-item" type="button" onClick={this.onClick}>{value}</button>})

In other words: replace the class attribute with className . 换句话说:将class属性替换为className

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

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