Skip to content

Instantly share code, notes, and snippets.

@madflanderz
Created February 20, 2019 15:06
Show Gist options
  • Save madflanderz/dc185464344c9cd48877a9383fcce862 to your computer and use it in GitHub Desktop.
Save madflanderz/dc185464344c9cd48877a9383fcce862 to your computer and use it in GitHub Desktop.
useValueChanged hook
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