Skip to content

Instantly share code, notes, and snippets.

@mic-css
Last active May 6, 2020 10:22
Show Gist options
  • Save mic-css/3230b49d1337e64f66135f91f6294c47 to your computer and use it in GitHub Desktop.
Save mic-css/3230b49d1337e64f66135f91f6294c47 to your computer and use it in GitHub Desktop.
Custom hook that keeps track of the previous value of an object or primitive, useful for comparing props between renders
import { useEffect, useRef } from 'react'
export function usePrevious<T>(value: T) {
const ref = useRef<T>()
useEffect(() => {
ref.current = value
})
return ref.current
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment