0

can you help me understand why this onClick event does not bind to the button and won't fire?

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.

3
  • You could simplify this code a lot - all those divs add a lot of extra noise. I think you have a syntax error somewhere. Have you tried linting it via jsxhint filename.js ? In your constructor, this.props.allValues should be props.allValues Commented Jan 25, 2018 at 16:21
  • 1
    class is not a valid property in React - you probably mean className. Apart from that, your code works fine for me. Hitting the buttons makes the console log "here" as expected. Commented Jan 25, 2018 at 16:23
  • className was indeed the problem. Commented Jan 25, 2018 at 16:27

1 Answer 1

1
 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.