Calling a javascript function in JSX: why does calling a function without the () work?

喜你入骨 提交于 2019-11-30 15:55:38

Without parenthesis, you're not calling the function. The name of the function without the parenthesis is a reference to the function. Parentheses is not used in the function at that point because we are not calling the function at the point where the code is encountered, but instead want to pass a reference to the function. If you use makeDoggy() , the function will get called at that point, we instead want it to be called only after onClick, so we pass a reference to makeDoggy there.

Alternatively you can do onClick={()=>makeDoggy()}

e gets bound automatically using something called property initializer in es6.

No need for too much stress the solution is just double quote elimination "", in ReactJs to call a function inside an element you just have to do the following

onClick={functionName}

React can process function in curly braces.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!