Created
February 20, 2019 08:50
-
-
Save oriSomething/08868990f2f07ac7e17a04a9661a84ae to your computer and use it in GitHub Desktop.
React hook - usePreviousValue
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
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