Here's an example of how you might handle a button click event:
class MyComponent extends React.Component { handleClick() { console.log("Button was clicked!"); } render() { return ( <button onClick={this.handleClick}> Click me </button> ); } }
In the example above, the handleClick
method is passed as the onClick
prop to the button element. When the button is clicked, the handleClick
method will be called, which will log a message to the console.
It's important to note that in this example the handleClick
function is a class method, so it should be defined as this.handleClick = this.handleClick.bind(this)
in the constructor to have access to this
keyword inside the function or you can use arrow function handleClick = () => {...}
which automatically binds this
keyword.
Also, React events are named in camelCase, unlike the DOM events which are in lowercase, for example onClick, onChange, onSubmit are React events and onclick, onchange, onsubmit are DOM events.
Social Plugin