Last active
November 9, 2022 14:39
-
-
Save dmorenogogoleva/4a503a539ae94c5f4acc5547ca791adc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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