Skip to content

Instantly share code, notes, and snippets.

@chsilva
Created June 10, 2021 13:21
Show Gist options
  • Save chsilva/d105a42fa5189a49e96dfadb8920ba0e to your computer and use it in GitHub Desktop.
Save chsilva/d105a42fa5189a49e96dfadb8920ba0e to your computer and use it in GitHub Desktop.
this is a custom hook that works like a `didUpdate` lifecycle method in react with typescript
import { useEffect, useRef } from 'react'
function useDidUpdateEffect(fn: () => void, dependencies: any[]): void {
const mountRef = useRef<boolean>(false)
useEffect((): void => {
if (mountRef.current) {
fn()
} else {
mountRef.current = true
}
}, dependencies)
}
export { useDidUpdateEffect }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment