Skip to content

Instantly share code, notes, and snippets.

@hawx1993
Created November 7, 2022 10:30
Show Gist options
  • Select an option

  • Save hawx1993/c063351032fbe77371b8f7f049a2f2d4 to your computer and use it in GitHub Desktop.

Select an option

Save hawx1993/c063351032fbe77371b8f7f049a2f2d4 to your computer and use it in GitHub Desktop.
function usePrevious(value) {
const ref = useRef();
useEffect(() => {
ref.current = value; //assign the value of ref to the argument
}, [value]); //this code will run when the value of 'value' changes
return ref.current || {}; //in the end, return the current ref value.
}
export { usePrevious };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment