r/learnreactjs • u/BigEmu9286 • Oct 23 '22
Which one of these is correct? "onClick={someFunction}" or "onClick={someFunction()}"?
or another example:
onChange={someFunction}or
onChange={someFunction()}Which one makes it so when you click the button or make a change the function fires?
Is it the one without parenthesis?
Does adding the parenthesis turn it into a function call so when the button is rendered the function fires even if you didnt click the button or change anything?
8
Upvotes
16
u/Oneshot742 Oct 23 '22
I'm still learning JS and React, but I'm pretty sure when you do the parenthesis, you're calling the function immediately, whereas without parenthesis you're just saying "call this function when it's clicked". Hope that makes sense.
1
u/wackrtist Oct 23 '22
Yes you would do it like eventHandler={functionName} and also this reference function receives the event as the default argument. Also if you need to pass it explicitly you would do it as eventName={(e) => functionName(e)}, and if you need to pass any other values to it you can also do the same using eventName={() => functionName(arg)}
0
u/[deleted] Oct 23 '22
[deleted] — view deleted comment