1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-27 10:32:58 +02:00
joplin/packages/lib/hooks/useNowEffect.ts

29 lines
819 B
TypeScript
Raw Normal View History

import type * as React from 'react';
import shim from '../shim';
type CleanupCallback = (()=> void)|null;
export type EffectFunction = ()=> CleanupCallback;
const { useRef, useMemo, useEffect } = shim.react();
// Like useEffect, but runs as soon as possible.
const useNowEffect = (effect: EffectFunction, dependencies: React.DependencyList) => {
const lastCleanup = useRef<CleanupCallback>(null);
const cleanupCallback = useMemo(() => {
lastCleanup.current?.();
lastCleanup.current = null;
return effect() ?? null;
// eslint-disable-next-line @seiyab/react-hooks/exhaustive-deps -- This is a custom hook
}, dependencies);
lastCleanup.current = cleanupCallback;
useEffect(() => {
return () => {
lastCleanup.current?.();
lastCleanup.current = null;
};
}, []);
};
export default useNowEffect;