Centralize calling toasts and add imperative API

This commit is contained in:
trevor-signal 2023-10-19 14:59:21 -04:00 committed by GitHub
parent dd45b08b0f
commit 0c896ca1f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 568 additions and 158 deletions

28
ts/hooks/useIsMounted.ts Normal file
View file

@ -0,0 +1,28 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { useCallback, useEffect, useRef } from 'react';
/**
* If you get a warning like:
*
* Warning: Can't perform a React state update on an unmounted component.
*
* your component is probably trying to set state after it has unmounted, e.g. after a
* timeout or async call. If you can, clear the timeout when the component unmounts (e.g.
* on useEffect cleanup). Otherwise, use this hook to check if the component is mounted
* before updating state.
*/
export function useIsMounted(): () => boolean {
const isMounted = useRef(false);
useEffect(() => {
isMounted.current = true;
return () => {
isMounted.current = false;
};
}, []);
return useCallback(() => isMounted.current === true, []);
}