Created
February 20, 2019 15:06
-
-
Save madflanderz/dc185464344c9cd48877a9383fcce862 to your computer and use it in GitHub Desktop.
useValueChanged hook
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 { useRef } from 'react' | |
function useValueChanged<T>(callback: (value: T) => void, value: T) { | |
// save initial value on first call | |
const ref = useRef(value) | |
// execute callback when value has changed | |
if (ref.current !== value) { | |
ref.current = value | |
callback(value) | |
} | |
} | |
export default useValueChanged |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment