【问题标题】:difference between {foo()} and {foo}{foo()} 和 {foo} 之间的区别
【发布时间】:2020-01-25 08:08:08
【问题描述】:

当用户单击按钮时,我遇到了一个 JSX 表达式,必须执行一个函数。 我想知道引用函数和使用'()'调用函数的区别

例如什么时候用哪个?

1)

       <button onClick={foo}>Something here</button>

2)

       <button onClick={foo()}>Something here</button>

和函数 foo

       const foo = () => {
          console.log('something here');            
        } 

【问题讨论】:

    标签: jsx


    【解决方案1】:

    使用

    <button onClick={foo()}>Something here</button>
    

    每次渲染组件都会调用函数foo。

    使用

    <button onClick={foo}>Something here</button>
    

    只有当按钮被点击时才会调用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-01
      • 2017-07-04
      • 2016-09-09
      • 2012-07-26
      • 2017-03-14
      • 2017-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多