Created
June 10, 2021 13:21
-
-
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
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 { 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