最佳答案
React Hooks 提供了 useState 选项,我经常看到 Hooks 与 Class-State 的比较。那钩子和一些常规变量呢?
比如说,
function Foo() {
let a = 0;
a = 1;
return <div>{a}</div>;
}
我没有使用 Hooks,它会给我同样的结果:
function Foo() {
const [a, setA] = useState(0);
if (a != 1) setA(1); // to avoid infinite-loop
return <div>{a}</div>;
}
那么这有什么区别呢? 在这种情况下使用 Hooks 更加复杂... ... 那么为什么要开始使用它呢?