Skip to content

Instantly share code, notes, and snippets.

@dmorenogogoleva
Last active November 9, 2022 14:39
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 dmorenogogoleva/4a503a539ae94c5f4acc5547ca791adc to your computer and use it in GitHub Desktop.
Save dmorenogogoleva/4a503a539ae94c5f4acc5547ca791adc to your computer and use it in GitHub Desktop.
export function usePreviousPersistent<T extends unknown>(value: T) {
const ref = useRef<{ value: T; prev: T | null }>({
value,
prev: null,
});
const currentValue = ref.current.value;
if (!isEqual(value, currentValue)) {
ref.current = {
value,
prev: currentValue,
};
}
return ref.current.prev;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment