Skip to content

Instantly share code, notes, and snippets.

@oriSomething
Created February 20, 2019 08:50
Show Gist options
  • Save oriSomething/08868990f2f07ac7e17a04a9661a84ae to your computer and use it in GitHub Desktop.
Save oriSomething/08868990f2f07ac7e17a04a9661a84ae to your computer and use it in GitHub Desktop.
React hook - usePreviousValue
import * as React from "react";
export function usePreviousValue<T>(value: T): T {
const refPrevious = React.useRef(value);
const refCurrent = React.useRef(value);
if (refPrevious.current !== refCurrent.current) {
refPrevious.current = refCurrent.current;
}
if (refCurrent.current !== value) {
refCurrent.current = value;
}
return refPrevious.current;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment