Centralize calling toasts and add imperative API
This commit is contained in:
parent
dd45b08b0f
commit
0c896ca1f2
11 changed files with 568 additions and 158 deletions
28
ts/hooks/useIsMounted.ts
Normal file
28
ts/hooks/useIsMounted.ts
Normal 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, []);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue