Declaring inline event handlers
The typical approach to assigning handler functions to JSX properties is to use a named function. However, sometimes, you might want to use an inline function, where the function is defined as part of the markup. This is done by assigning an arrow function directly to the event property in the JSX markup:
import * as React from "react"; class MyButton extends React.Component { render() { return ( <button onClick={(e) => console.log("clicked", e)}> {this.props.children} </button> ); } } export default MyButton;
The main use of inlining event handlers like this is when you have a static parameter value that you want to pass to another function. In this example, you're calling console.log()
with the clicked string....