packages/@fbcmobile-ui/Hooks/useMountedState.js (12 lines of code) (raw):
/**
* Copyright (c) Facebook, Inc. and its affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
'use strict';
import {useCallback, useEffect, useRef} from 'react';
export default (): (() => boolean) => {
const mountedRef = useRef<boolean>(false);
useEffect(() => {
mountedRef.current = true;
return () => {
// The cleanup function of useEffect is called by React on unmount
mountedRef.current = false;
};
}, []);
return useCallback(() => mountedRef.current, []);
};