Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F99387842
useMounted.js
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, Jan 24, 02:42
Size
913 B
Mime Type
text/x-java
Expires
Sun, Jan 26, 02:42 (1 d, 20 h)
Engine
blob
Format
Raw Data
Handle
23788462
Attached To
rOACCT Open Access Compliance Check Tool (OACCT)
useMounted.js
View Options
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 () {
mounted.current = true;
return function () {
mounted.current = false;
};
}, []);
return isMounted.current;
}
Event Timeline
Log In to Comment