1. First Soltioun
using inline arrow function
In below example alert is placed inside arrow function assigned directly to the button's onClick property.
//Note: Uncomment import lines during working with JSX Compiler.
// import React from react';
// import ReactDOM from react-dom';
const App = () => {
return (
<div >
<button onClick={() => alert('button click catched')}>Click me</button>
</div>
);
};
const root = document.querySelector('#root');
ReactDOM.render(<App />, root );
2. Second solution
using event handling function
In below example we create external handleClick arrow function which displays an alert. Then we pass handleClick function to the button's onClick property which handles mouse click event.
// import React from react';
// import ReactDOM from react-dom';
const handleClick = () => {
alert('button click catched');
};
const App = () => {
return (
<div >
<button onClick={handleClick}>Click me</button>
</div>
);
};
const root = document.querySelector('#root');
ReactDOM.render(<App />, root );