How to Call a Function inside a Render in React/Jsx

前端 未结 3 1044
谎友^
谎友^ 2020-12-07 15:31

I want to call a function inside some embedded html. I tried the following but the function isn\'t called. Would this be the incorrect way of calling a function inside a ren

3条回答
  •  清歌不尽
    2020-12-07 16:24

    The fix was at the accepted answer. Yet if someone wants to know why it worked and why the implementation in the SO question didn't work,

    First, functions are first class objects in JavaScript. That means they are treated like any other variable. Function can be passed as an argument to other functions, can be returned by another function and can be assigned as a value to a variable. Read more here.

    So we use that variable to invoke the function by adding parentheses () at the end.

    One thing, If you have a function that returns a funtion and you just need to call that returned function, you can just have double paranthesis when you call the outer function ()().

提交回复
热议问题