4

Whenever I pass function parentheses in onClick of button, it automatically calls when page loads even without clicking on button. But when I don't pass function parentheses in onClick of button, it calls only when button is clicked.

With passing parentheses in function call

<button onClick={this.handleButtonClick()}>Increment</button>

Without passing parentheses in function call

<button onClick={this.handleButtonClick}>Increment</button>

Why function called automatically on page load whenever I pass parentheses to function even-though it is written inside onClick of button ?

1
  • 1
    you can still do method if you bind it (you can easily achieve it now using arrow binding): onClick={() => this.handleButtonClick()}, then it will not calls automatically when page loads Commented Jul 24, 2019 at 9:06

5 Answers 5

6

There's a difference between a function call and a function.

When a component renders, all statements are executed and expressions are evaluated. It's plain javascript, that's how javascript works.

this.handleButtonClick() is a function call, and therefore the function will be called once the component renders. I assume that handleButtonClick() returns undefined which will cause the button to render as <Button onClick={undefined} />. Now, if onClick is undefined, nothing will happen when the click actually happens as there is no function to be called.

this.handleButtonClick is just a reference to a function, it doesn't invoke it. You need to pass the function reference to onClick prop so that it can be called later by React, once the click actually happens.

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

Comments

1

It automatically calls because you are invoking it immediately by:

this.handleButtonClick()

"onClick" does not work like that, if you want to manually invoke the function you can use:

onClick={() => this.handleButtonClick()}

This is done automatically if you use function reference. Hence, you don't need to invoke it or use a callback function. Actually, using function reference is better since that function isn't recreated in every render.

4 Comments

Using function reference instead of an inline arrow function is better*. Furthermore, In functional components you should use useCallback to prevent the function reference from changing. In class components, functions should be methods of a class, like the OP has already done.
@AnteGulin, well, yes. I tried to explain it in my last paragraph with the reason (recreated in every render) :) "...you should use useCallback" Maybe not always? :) kentcdodds.com/blog/usememo-and-usecallback
You can still have a reference to a function that is created in the render method, it's still a reference, but the one that will change in every render too, hence why I commented to clarify further.
Oh, totally misunderstood. Yes, you are right about that. Using reference doesn't matter in functional components. Since OP uses this I explained it as they use class components and thought that you are giving extra information about functional components by saying "should use useCallback" as a general manner.
0

There is a difference between function reference and function invoking.

When you set the dynamic value like: this.handleButtonClick() that means you're invoking the function as soon as the component renders to the page.

But when you just pass the reference of the function like this.handleButtonClick that means React will call this function only when the user clicks on it.

Comments

0

JS will execute the function On each Render when you do OnClick(Func(params))

because you are calling a function instead of passing it.

Do this instead :

OnClick ( ()=> {Func(params) } )

1 Comment

I think you missed }. Can you check your syntax?
0

In react functional components, if we pass any function to onclick this way.. onClick={func()} this func will be called right away when the page is loaded, that for sure we don't want.

So we have two ways to make it work right.

one, pass it as a reference like this- onClick={()=> func()} (this means only pass reference to that function when page loads, but call that func() if component is clicked over.)

second, just pass the name of the function- onClick={func} (this means both pass reference and call on Call).

these both things work the same way. second one is more easy however the former one is used the most.

Comments

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.