Created
April 23, 2021 23:17
-
-
Save dggodfrey/1389f405a8fb697991a7dfcaf562ac88 to your computer and use it in GitHub Desktop.
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, EffectCallback, DependencyList} from 'react'; | |
type useNonInitialEffectReturn = void | (() => void | undefined) | |
export const useNonInitialEffect = (effect: EffectCallback, deps?: DependencyList): useNonInitialEffectReturn => { | |
const initialRender = useRef(true); | |
useEffect(() => { | |
let effectReturns: useNonInitialEffectReturn = () => { /* Empty Return fallback */ }; | |
if (initialRender.current) { | |
initialRender.current = false; | |
} else { | |
effectReturns = effect(); | |
} | |
if (effectReturns && typeof effectReturns === 'function') { | |
return effectReturns; | |
} | |
}, deps); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment