/* you have to define an event handler before you can pass one anywhere. How do you define an event handler in React? You define an event handler as a method on the component class, just like the render method. */ class Example extends React.Component { handleEvent() { alert(`I am an event handler. If you see this message, then I have been called.`); } render() { return ( <h1 onClick={this.handleEvent}> Hello world </h1> ); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter