0

This is following a tutorial in React JS.

  class Square extends React.Component {
    render() {
        return (
        <button
            className="square"
            onClick={() => this.props.onClick()}
        >
            {this.props.value}
        </button>
        );
    }
  }

   function Square(props) {
        return (
            <button className="square" onClick={props.onClick}>
            {props.value}
            </button>
        );
    }    

above components do the same thing.there's a statement in the tutorial as follows

"When we modified the Square to be a function component, we also changed onClick={() => this.props.onClick()} to a shorter onClick={props.onClick} (note the lack of parentheses on both sides)."

I don't understand why there aren't parentheses on the right side in the function component? is it not a function call?

2 Answers 2

1

is it not a function call?

No, it isn't. It is a function. You are passing it to be the value of the onClick property.

This is a function that does nothing except call foo and return its value.

() => foo()

This is the function foo:

foo

The net result is more-or-less the same unless foo cares about the value of this or the arguments it gets.


If you added () to the short version, you would call the function immediately and pass the return value to onClick. Since that return value (probably) isn't a function, and you don't want the function to run until the click happens, that wouldn't be helpful.


function run_me(a_function) {
    a_function();
}

function foo() {
    console.log("foo has run");
}

run_me(foo);
run_me(() => foo());
run_me(foo());

Sign up to request clarification or add additional context in comments.

2 Comments

That's right about callback concept in js, ok, but lets me return to react and ask again: why in case of the component class we had to assign returned value of onClick function, but in case of function component, we had to the reference of onClick function?
"why in case of the component class we assigned returned value of onClick function" — You have two examples on onClick={someValue} in your code. In the first case, the value is a new arrow function which calls one function (() => this.props.onClick()). In the second case you have an existing function (props.onClick). You are not calling a function and passing its return value to onClick={here}.
0

When passing a handler we actually need to pass a reference to a function.

I don't understand why there aren't parentheses on the right side in the function component? is it not a function call?

When you put parentheses you are invoking the function.
So when doing this:

onClick={props.onClick()}

We are passing the returned value of that function instead of a reference to it.

5 Comments

While true, that ignores the () => in the example with the parentheses at the end so doesn't really describe the situation here.
hmm, are you talking about the class example? isn't that falls under passing a function reference, or because it's anonymous you think that's different?
My point is that onClick={() => this.props.onClick()} and onClick={props.onClick} both pass a function to onClick., and neither invoke it immediately.
Indeed, that is my point as well. AFAIK, the OP asks why we are not doing onClick={props.onClick()} so this is what i was trying to explain. i guess i wasn't communicating clearly with my answer
Ahhhh. I see now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.