Download wp-calypso/apps/notifications/src/panel/helpers/use-safe.js from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 525 Bytes
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/apps/notifications/src/panel/helpers/use-safe.js
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/wp-calypso/apps/notifications/src/panel/helpers/use-safe.js
-
curl -L -o use-safe.js https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/apps/notifications/src/panel/helpers/use-safe.js
525 Bytes
| import { useCallback, useRef, useLayoutEffect } from 'react'; | |
| /** | |
| * Hook to be used to make sure a function `fn` is called only | |
| * if the component which uses it is still mounted. | |
| * @param {Function} fn A function you want to be safe to call | |
| */ | |
| export default function useSafe( fn ) { | |
| const mounted = useRef( false ); | |
| useLayoutEffect( () => { | |
| mounted.current = true; | |
| return () => ( mounted.current = false ); | |
| }, [] ); | |
| return useCallback( ( ...args ) => ( mounted.current ? fn( ...args ) : void 0 ), [ fn ] ); | |
| } | |