我尝试单击按钮以测试它是否被单击? 但是当我单击它时,它没有任何反应,是否丢失了东西?
import React, { Component } from 'react';
import './App.css';
class App extends Component {
switchNameHandler = () =>{
console.log('was clicked');
};
render() {
return (
<div className="App">
<h1>Hi, I'm a React App</h1>
<h1>Another heading</h1>
<button onClick={this.switchNameHandler()}>Switch Names</button>
</div>
);
}
}
export default App;
答案 0 :(得分:1)
尝试将按钮更改为:
<button onClick={() => this.switchNameHandler()}>Switch Names</button>
当单击按钮时,这应该调用该呼叫