React-无法调用的表达式

我在React中设置了以下代码。

const useToggle = (initialValue : boolean) => {
  const [bool, setBool] = useState(initialValue)
  const toggle = () => {
    setBool(!bool)
  }
  return [bool, toggle]
}

const Test : FC = () => {
  const [bool, toggle] = useToggle(false)  
  return (    
    <div>
      boolean value: {bool}
      <button onClick={() => {
        toggle()
      }}>Toggle</button>

    </div>

  )
}

On the line with toggle() I'm getting the following error:

This expression is not callable. Not all constituents of type 'boolean | (() => void)' are callable. Type 'false' has no call signatures.

However, the error goes away if I change the <button> element to this:

<button onClick={() => toggle}>

我不太明白区别是什么。究竟如何解决错误?

评论