Javascript 反应:调用函数,为什么第一个可以工作而第二个不能?

Javascript 反应:调用函数,为什么第一个可以工作而第二个不能?,javascript,reactjs,func,Javascript,Reactjs,Func,也许这是一个愚蠢的问题,但我想知道为什么 const isDisabled = () => // condition check 这项工作: <button type="button" disabled={this.isDisabled()} > Let Me In </button> 让我进去 但这不起作用: <button type="button" disabled={this.isDisabled} >

也许这是一个愚蠢的问题,但我想知道为什么

const isDisabled = () => 
  // condition check
这项工作:

<button
  type="button"
  disabled={this.isDisabled()}
>
  Let Me In
</button>

让我进去
但这不起作用:

<button
   type="button"
   disabled={this.isDisabled}
>
  Let Me In
</button>

让我进去

因为禁用的
需要是布尔值:


当您添加这样的括号.isDisabled()时,将执行函数,并将返回值分配给按钮的disabled属性。 当您只是说
disabled=this.isDisabled
时,您只是传递函数的引用,因此不会计算该值


还要记住,按钮上的“禁用”属性是一个布尔值。因此,检查从函数返回的值。

在第一个示例中,使用
this.isDisabled()
调用函数。
在第二个示例中,使用
this.isDisabled
传递函数引用而不调用

尝试
console.log()
第二种方法的工作原理与变量类似。例如:

var myFunction = this.isDisabled; // I've put the reference to myFunction
myFunction(); // now I've called isDisabled()

因为第二个函数返回函数,而attr disabled需要booleanUse
React.useState