I just wanted to know how does this work, if I have an onChange()
onChange={() => input()}
The above code immediately executes the input function when onChange() is triggered.
But what about this, how does this work?
onChange={() => input}
Even when onChange is triggered the input function is not being called.
What is the difference between these two statements?
inputis a reference to theinputfunction where asinput()executes theinputfunction and returns the value.So when you call an arrow function without explicit brackets (
{}), the value is automatically returned.So
onChange={() => input}will get the reference of theinputfunction, where asonChange={() => input()}will get the return value of theinputfunction.Someone recommended
onChange={input}in the comments, this will automatically pass all the parameters given toonChange(like the DOM event) to theinputfunction, keep that in mind an this equivalent toonChange={(e) => input(e)}This is fine for
onChangeof (eg) an<input>element, but one common mistake is by using theparseIntfunction, if you pass all the arguments to that, it will use it as base and give unexpected output:React demo showing the 4 cases shown above, please see the comments in the code regarding the working