React学习

useEffect

是每次组件 render 完后判断依赖并执行

useCallback缓存一个函数

1
2
3
4
5
6
7
8
9
function Counter() {
const [count, setCount] = useState(0);
const handleIncrement = useCallback(
() => setCount(count + 1),
[count], // 只有当 count 发生变化时,才会重新创建回调函数
);
// ...
return <button onClick={handleIncrement}>+</button>
}
阅读更多