设置处理程序函数的方式不正确。您还可以在构造函数中绑定,并在按钮的onclick事件内部嵌入一个箭头函数。你只需要在一个地方装订。
看一看我做的代码沙盒示例,这样您就可以看到如何声明一个类方法处理程序函数并将其与click事件一起使用。注意,这里没有要绑定onclick事件的构造函数或arrow函数吗?这是因为绑定发生在类方法上。
handleClick = () => {}
class App extends React.Component {
handleClick = e => {
console.log(e.target + " was clicked.");
// Do whatever functionality you need here.
// In your example you do not show that it matters what the element is,
// so you don't need to pass the event (e) into your class method.
};
render() {
return (
<Container>
<Divider hidden />
<Button content="Click Me" onClick={this.handleClick} />
<Divider hidden clearing />
<Message info>
Look in your console and you will see that the click function is
working.
</Message>
</Container>
);
}
}
Here is a working codesandbox example.