mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-24 10:27:10 +02:00
29 lines
819 B
TypeScript
29 lines
819 B
TypeScript
|
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;
|