1 import { useCallback, useState } from 'react';
3 import useIsMounted from '@proton/hooks/useIsMounted';
4 import type { Callback, Maybe } from '@proton/pass/types';
6 export const useEnsureMounted = () => {
7 const isMounted = useIsMounted();
9 <T extends Callback>(fn: T) =>
10 ((...args: Parameters<T>): Maybe<ReturnType<T>> => {
11 if (isMounted()) return fn(...args);
17 export const useMountedState = <T>(initial: T) => {
18 const [state, setState] = useState<T>(initial);
19 const ensureMounted = useEnsureMounted();
20 const setStateSafe = useCallback(ensureMounted(setState), []);
22 return [state, setStateSafe] as const;