javascript - React: onClick on button not working - Stack Overflow

For the past hour, I have been trying to get this to work.onClick on any of the buttons is not working

For the past hour, I have been trying to get this to work. onClick on any of the buttons is not working at all… What am I doing wrong?

const React = require('react');

class Questionnaire extends React.Component {
constructor (props) {
    super(props);
    this.state = { selectedSection: 0 };
}

selectSection (e) {
    console.log(e);
}

render () {
    return (
        <div>
            <div className='btn-group mr-2' role='group'>
                <button className='btn btn-primary' onClick={this.selectSection}>A</button>
                <button className='btn btn-secondary' onClick={this.selectSection}>B</button>
                <button className='btn btn-secondary' onClick={this.selectSection}>C</button>
                <button className='btn btn-secondary' onClick={this.selectSection}>D</button>
          </div>
        </div>
    );
}
}

 module.exports = Questionnaire;

I should mention that I am using it with express-react-views

For the past hour, I have been trying to get this to work. onClick on any of the buttons is not working at all… What am I doing wrong?

const React = require('react');

class Questionnaire extends React.Component {
constructor (props) {
    super(props);
    this.state = { selectedSection: 0 };
}

selectSection (e) {
    console.log(e);
}

render () {
    return (
        <div>
            <div className='btn-group mr-2' role='group'>
                <button className='btn btn-primary' onClick={this.selectSection}>A</button>
                <button className='btn btn-secondary' onClick={this.selectSection}>B</button>
                <button className='btn btn-secondary' onClick={this.selectSection}>C</button>
                <button className='btn btn-secondary' onClick={this.selectSection}>D</button>
          </div>
        </div>
    );
}
}

 module.exports = Questionnaire;

I should mention that I am using it with express-react-views

Share Improve this question edited Aug 14, 2017 at 16:56 Navarjun asked Aug 14, 2017 at 16:49 NavarjunNavarjun 611 gold badge1 silver badge10 bronze badges 6
  • 1 Are there any console errors? – Sumner Evans Commented Aug 14, 2017 at 16:51
  • No console errors – Navarjun Commented Aug 14, 2017 at 16:52
  • 1 The code seems to be working fine for me. Nothing's showing up on your end? – Iruss Commented Aug 14, 2017 at 16:54
  • Yeah! No logs when I click on any of the buttons. – Navarjun Commented Aug 14, 2017 at 16:55
  • 1 From express-react-views readme: "It renders static markup and does not support mounting those views on the client." github./reactjs/express-react-views/issues/59 – CD.. Commented Aug 14, 2017 at 16:58
 |  Show 1 more ment

3 Answers 3

Reset to default 1

You need to bind the function to the ponent like so:

class Questionnaire extends React.Component {
    constructor (props) {
        super(props);
        this.state = { selectedSection: 0 };

        this.selectSection = this.selectSection.bind(this)
    }

Keep everything else the same

Try This

    const React = require('react');

    class Questionnaire extends React.Component {
    constructor (props) {
        super(props);
        this.state = { selectedSection: 0 };
    }

    selectSection = function(e){
    console.log(e);
  }

    render () {
        return (
            <div>
                <div className='btn-group mr-2' role='group'>
                    <button className='btn btn-primary' onClick={this.selectSection}>A</button>
                    <button className='btn btn-secondary' onClick={this.selectSection}>B</button>
                    <button className='btn btn-secondary' onClick={this.selectSection}>C</button>
                    <button className='btn btn-secondary' onClick={this.selectSection}>D</button>
              </div>
            </div>
        );
    }
    }

You need to bind the method to the class. Add this line to the constructor:

this.selectSection = this.selectSection.bind(this);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745356027a4624122.html

相关推荐

  • javascript - React: onClick on button not working - Stack Overflow

    For the past hour, I have been trying to get this to work.onClick on any of the buttons is not working

    5小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信