mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-30 10:36:35 +02:00
4a88d6ff7a
Co-authored-by: Laurent Cozic <laurent22@users.noreply.github.com>
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;
|