In the original post, there's already a post on function() vs function.
In summary (and in layman terms), though, there are 2 ways on how you handle onClick.
When you want to pass some value to the function
//the button
<button onClick={() => myFunction('I am a click')}>Button</button>
//the function
const myFunction = (value) => {
console.log(value) //prints 'I am a click'
}
When you do NOT need to pass any value (events/default props will be passed by default)
//the button
<button onClick={myEvent}>Button</button>
//the function
const myFunction = (event) => {
console.log(event) //prints the button element
}
Creating a custom Button component with custom props.
const MyButton = ({ onClick }) => {
const customOnClick = () => {
onClick('Somebody', 'Special'); //we call the onclick here
}
return <button onClick={customOnClick}>Click Me</button>
}
const App =() => {
//the onClick in MyButton component actually triggers this function.
const onClick = (value, value2) => {
console.log(value,value2) //prints "Somebody Special"
}
return (<MyButton onClick={onClick} />) // i do not need to specify the props to pass
}