import { useLayoutEffect , useRef } from "react" ; import useLatest from "../useLatest" ; import { CleanupCallback } from "../shared" ; export type ConditionalLayoutEffectCallback = ( ) => CleanupCallback | void ; function useConditionalLayoutEffect ( callback , shouldExecute = false ) { const truthyCount = useRef ( 0 ) ; const callbackRef = useLatest ( callback ) ; if ( Boolean ( shouldExecute ) ) { truthyCount . current ++ ; } useLayoutEffect ( ( ) => { if ( truthyCount . current === 0 ) { return ; } return callbackRef . current ( ) ; } , [ truthyCount . current ] ) ; } export default useConditionalLayoutEffect ;