Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
62
rated 0 times [  66] [ 4]  / answers: 1 / hits: 8044  / 4 Years ago, thu, july 16, 2020, 12:00:00

What's the difference between this code:


<button onClick={()=>props.submitHandler(searchInputValue)}>Submit</button>

and


<button onClick={props.submitHandler(searchInputValue)}>Submit</button>


The difference is the first one has the parentheses and the second one doesn't. Without the parentheses, my app seems to be re-render indefinitely. Can someone kindly explain it to me?


More From » reactjs

 Answers
4

In first one:


<button onClick={()=>props.submitHandler(searchInputValue)}>Submit</button>

This is arrow function and it will trigger only onClick of the button.


In second one:


<button onClick={props.submitHandler(searchInputValue)}>Submit</button>

This is a normal function call , which calls the method as soon the rendering of the component happens.


[#3162] Monday, July 13, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leog

Total Points: 225
Total Questions: 113
Total Answers: 118

Location: Oman
Member since Wed, Apr 12, 2023
1 Year ago
leog questions
Mon, May 11, 20, 00:00, 4 Years ago
Sat, Apr 18, 20, 00:00, 4 Years ago
Sun, Apr 5, 20, 00:00, 4 Years ago
;