0

This is the correct way.

function Button() {

    return (

        <button onClick={() => console.log("Hello World")}>Click me!</button>

    )

}

export default Button;

But why directly pass console.log like this is not allowed?

function Button() {

    return (

        <button onClick={console.log("Hello World")}>Click me!</button>

    )

}

export default Button;

I just want to know why

dtnwen
  • 1
  • 1
  • 1
    Because the second form **calls** `console.log` and then uses its return value as the value of the `onClick` property, exactly like `a = example()` **calls** `example` and sets `a` to its return value. You need to provide a function. – T.J. Crowder Nov 07 '22 at 17:15

0 Answers0