useState 钩子和 useReactiveVar(反应变量)钩子有什么区别?

时间:2021-06-18 19:00:00

标签: reactjs apollo reactive-variable

下面两段代码基本上有什么区别?我们可以说 useReactiveVar 是一种具有重新渲染能力的 useRef 吗?

  1. useState(此日志会记录“iAmAwesome”吗?)
const [awesomeVar, setAwesomeVar] = useState("iAmAwesome");
setTimeout(() => console.log(awesomeVar), 1000);
setAwesomeVar("youAreAwesome");
  1. useReactiveVar(此日志会记录“iAmAwesome youAwesome”吗?)
const reactiveVar = makeVar("iAmAwesome");
// ...
const awesomeVar = useReactiveVar(reactiveVar);
setTimeout(() => console.log(awesomeVar, useReactiveVar()), 1000);
useReactiveVar("youAreAwesome");

0 个答案:

没有答案