Skip to content

Instantly share code, notes, and snippets.

@LouisPetrik
Last active August 8, 2021 11:14
Show Gist options
  • Save LouisPetrik/b1323bd07b455168dcb51b865e1407b4 to your computer and use it in GitHub Desktop.
Save LouisPetrik/b1323bd07b455168dcb51b865e1407b4 to your computer and use it in GitHub Desktop.
A custom hook for checking if a component already had it's initial render. Useful for building a hook-based componentDidUpdate() alternative.
import { useEffect, useRef } from 'react'
export const useDidMount = () => {
const didMountRef = useRef(false)
useEffect(() => {
didMountRef.current = true
}, [])
return didMountRef.current
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment