Skip to content

Instantly share code, notes, and snippets.

@danakt
Created December 3, 2018 13:09
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 danakt/75ac3fc09adb0a66e6962d31dcc846ac to your computer and use it in GitHub Desktop.
Save danakt/75ac3fc09adb0a66e6962d31dcc846ac to your computer and use it in GitHub Desktop.
React hook to use previous data
/**
* React hook to use previous data
*/
const usePrevious = function<T>(value: T): undefined | T {
const ref = React.useRef<T | undefined>(undefined);
React.useEffect(() => {
ref.current = value;
});
return ref.current;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment