onclick in react button code example
Example 1: react button onclick
import React, { Component } from 'react';
class App extends Component {
constructor(props) {
super(props);
this.sayHello = this.sayHello.bind(this);
}
sayHello() {
alert('Hello!');
}
return (
<button onClick={this.sayHello}>
Click me!
</button>
);
}
export default App;
Example 2: react onclick to make another button
import React from 'react';
const App = () => {
const message = () => {
console.log("Hello World!")
}
return (
<button onClick={message}> Press me to print a message! </button>
);
}
Example 3: react button onclick
import React from 'react';
function App() {
function sayHello() {
alert('Hello!');
}
return (
<button onClick={sayHello}>
Click me!
</button>
);
}
export default App;