如何在不调用的情况下传递 function/closure?

How to pass function/closure without calling it?

我正在尝试设置一个状态,它恰好是 react-native 中的一个函数,如下所示:

...
    setSendFunction(function () {
        console.log('HELLO');
    });

但每次运行此行时,都会打印“HELLO”,这是我不想要的。我只想将这个匿名函数存储在没有 运行 的地方。

正如@Bergi 所说,您想做一些类似的事情

import React, { useState } from "react";

export default function App() {
//here func() will give you the value of 5
  const [func, setFunc] = useState(() => () => 5);

  const handleClick = (num) => {
// after this, func() will provide new value, which is num
    setFunc((prevState) => () => prevState() + num);
  };
  return (
    <div>
      <h2>{func()} </h2>
      <button onClick={(e) => handleClick(10)}> Change State</button>
    </div>
  );
}