handleClick react 1

handleClick react

function ActionLink() {
  function handleClick(e) {    e.preventDefault();    console.log('The link was clicked.');  }
  return (
          Click me
    
  );
}

Here is what the above code is Doing:
1. The event handler is passed as a prop to the element.
2. When the
is clicked, the handleClick function is called.
3. The event object is passed as an argument to the handleClick function.
4. The event object contains information about the event that occurred.
5. The preventDefault() method is called on the event object to prevent the default action of the event from occurring.
6. The console.log() method is called to display a message in the console.

Note:

The event handler in the above example is defined as an arrow function, but it could also be defined as a regular function.

Similar Posts