Skip to content

Instantly share code, notes, and snippets.

@akullpp
Created February 8, 2024 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akullpp/71dfaf650f2d2bbe4de3ee88fe5b74cf to your computer and use it in GitHub Desktop.
Save akullpp/71dfaf650f2d2bbe4de3ee88fe5b74cf to your computer and use it in GitHub Desktop.
Use previous value hook for React
import { useEffect, useRef } from 'react';
const usePrevious = (value) => {
const ref = useRef();
useEffect(() => {
ref.current = value;
}, [value]);
return ref.current;
}
export { usePrevious }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment