Javascript ReactJS-事件不';t绑定(大型样板)

Javascript ReactJS-事件不';t绑定(大型样板),javascript,node.js,reactjs,Javascript,Node.js,Reactjs,我正在使用React 15.1.0,无法绑定事件。我不知道我做错了什么。这是我的组件: import React from 'react'; class PatientProfile extends React.Component { constructor(props) { super(props); this.state = {isToggleOn: true}; this.onClick = this.handleClick.bin

我正在使用React 15.1.0,无法绑定事件。我不知道我做错了什么。这是我的组件:

import React from 'react';


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

        this.state = {isToggleOn: true};
        this.onClick = this.handleClick.bind(this)
   }

    handleClick() {
        console.log('this is:');
    }

    render() {

        return (
          <div>
                <button onClick={this.onClick}>off</button>
          </div>
        );
    }
}

export default PatientProfile;
从“React”导入React;
类PatientProfile扩展了React.Component{
建造师(道具){
超级(道具);
this.state={istogleon:true};
this.onClick=this.handleClick.bind(this)
}
handleClick(){
log('this:');
}
render(){
返回(
关
);
}
}
导出默认PatientProfile;

我在网上搜索过,在React官方网站上试用过样本,但都没用。我是不是在安装的包中遗漏了什么?

我刚刚发现我应该构建bundle.js(npm run build),然后事件被绑定

我认为这样做的典型方式是,在构造函数中,使用
this.handleClick=this.handleClick.bind(this)
,在返回的jsx部分,使用
onClick={this.handleClick}
。您的代码很好,不会有任何问题。@Andrewillems我也尝试过这种方法,但它不起作用。@dfsq我也这么认为,但它不起作用,因此我认为安装的软件包可能存在一些问题。但是这个问题对您没有任何帮助。代码正在起作用,除了您之外,其他人都不知道。