This is a copy-n-paste from: Hooks FAQ usePrevious
function usePrevious(value) {
const ref = useRef();
useEffect(() => {
ref.current = value;
}); // no dependency arg, why?
return ref.current;
}
Since useEffect is called without a dependency array, its function argument will be called for every render. Is it safe to limit this to only when value changes by adding value to the useEffect dependency array, like this:
useEffect(() => {
ref.current = value;
}, [value]);
I'm wondering if there is some scenario where ref.current must updated every render, which would explain the omission of [value] from the documentation.