Debian Codegraph @

node/@patternfly/react-core/src/helpers/useUnmountEffect.ts

raw
1 import { EffectCallback, useEffect, useRef } from 'react';
2
3 /**
4 * A `useEffect`-like hook that only triggers when a component unmounts. Does not require a dependency list, as the effect callback will always be kept up to date.
5 */
6 export function useUnmountEffect(effect: EffectCallback) {
7 // Always use the latest effect callback so that it can reference the latest props and state.
8 const effectRef = useRef(effect);
9 effectRef.current = effect;
10
11 // Trigger the effect callback when the component unmounts.
12 useEffect(
13 () => () => {
14 effectRef.current();
15 },
16 []
17 );
18 }