usePrevious
获取上一轮的 props 或 state,考虑到这是一个相对常见的使用场景,很可能在未来 React 会内置此 Hook。
function usePrevious(value) {
const ref = useRef();
// 每一轮update,都会触发一次useEffect,从而为下一次更新value
useEffect(() => {
ref.current = value;
});
return ref.current;
}
function Counter() {
const [count, setCount] = useState(0);
const prevCount = usePrevious(count);
return (
<h1>
Now: {count}, before: {prevCount}
</h1>
);
}