mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-24 17:35:16 +00:00
34 lines
885 B
JavaScript
34 lines
885 B
JavaScript
|
import { useRef, useEffect } from 'react';
|
||
|
/**
|
||
|
* Track whether a component is current mounted. Generally less preferable than
|
||
|
* properlly canceling effects so they don't run after a component is unmounted,
|
||
|
* but helpful in cases where that isn't feasible, such as a `Promise` resolution.
|
||
|
*
|
||
|
* @returns a function that returns the current isMounted state of the component
|
||
|
*
|
||
|
* ```ts
|
||
|
* const [data, setData] = useState(null)
|
||
|
* const isMounted = useMounted()
|
||
|
*
|
||
|
* useEffect(() => {
|
||
|
* fetchdata().then((newData) => {
|
||
|
* if (isMounted()) {
|
||
|
* setData(newData);
|
||
|
* }
|
||
|
* })
|
||
|
* })
|
||
|
* ```
|
||
|
*/
|
||
|
|
||
|
export default function useMounted() {
|
||
|
var mounted = useRef(true);
|
||
|
var isMounted = useRef(function () {
|
||
|
return mounted.current;
|
||
|
});
|
||
|
useEffect(function () {
|
||
|
return function () {
|
||
|
mounted.current = false;
|
||
|
};
|
||
|
}, []);
|
||
|
return isMounted.current;
|
||
|
}
|