import { useCallback, useState } from 'react'; import { useMountedState } from 'react-use'; export const useSafeState = (initialValue: S | (() => S)) => { const isMounted = useMountedState(); const [state, _setState] = useState(initialValue); const setState = useCallback((...args) => { if (isMounted()) { _setState(...args); } }, [isMounted]); // This needs to happen to force a tuple return type const returnValue: [typeof state, typeof setState] = [state, setState]; return returnValue; };