handleClick
:import React from 'react'; const MyComponent = () => { const handleClick = () => { console.log('Button was clicked!'); } return ( <button onClick={handleClick}>Click me</button> ); } export default MyComponent;
In this example, the handleClick
function is defined inside the component and is passed as the onClick
event handler for a button element. When the button is clicked, the handleClick
function will be called and the message "Button was clicked!" will be logged to the console.
It's also possible to define function outside of the functional component and then pass it in as a props.
import React from 'react'; const handleClick = () => { console.log('Button was clicked!'); } const MyComponent = (props) => { return ( <button onClick={props.handleClick}>Click me</button> ); } export default MyComponent;
handleClick
is defined outside of the component and passed as a props to the component.
Social Plugin