UNPKG

@resourge/react-fetch

Version:

[![License](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)

43 lines (40 loc) 1.05 kB
/** * react-fetch v1.43.1 * * Copyright (c) resourge. * * This source code is licensed under the MIT license found in the * LICENSE.md file in the root directory of this source tree. * * @license MIT */ import { useEffect } from 'react'; const useOnFocus = (cb, onWindowFocus) => { useEffect(() => { if (onWindowFocus) { const { focus, blur, clear } = cb(); const visibilitychange = () => { if (document.visibilityState === 'visible') { focus(); } else { blur(); } }; window.addEventListener('focus', focus); window.addEventListener('blur', blur); window.addEventListener('visibilitychange', visibilitychange); return () => { clear(); window.removeEventListener('focus', focus); window.removeEventListener('blur', blur); window.removeEventListener('visibilitychange', visibilitychange); }; } }, [onWindowFocus]); }; export { useOnFocus }; //# sourceMappingURL=useOnFocus.js.map