From 6655092caed4d60f0bb23cc954cd35d63f1bec2f Mon Sep 17 00:00:00 2001 From: streamich Date: Wed, 27 Mar 2019 19:21:13 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20make=20sure=20all=20paths?= =?UTF-8?q?=20in=20usePageLeave=20return?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/usePageLeave.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/usePageLeave.ts b/src/usePageLeave.ts index a86f6e6cec..9a69a660e4 100644 --- a/src/usePageLeave.ts +++ b/src/usePageLeave.ts @@ -2,18 +2,18 @@ import {useEffect} from 'react'; const usePageLeave = (onPageLeave, args = []) => { useEffect(() => { - if (onPageLeave) { - const handler = (event) => { - event = event ? event : (window.event as any); - const from = event.relatedTarget || event.toElement; - if (!from || (from as any).nodeName === 'HTML') onPageLeave(); - }; + if (!onPageLeave) return; - document.addEventListener('mouseout', handler); - return () => { - document.removeEventListener('mouseout', handler); - }; - } + const handler = (event) => { + event = event ? event : (window.event as any); + const from = event.relatedTarget || event.toElement; + if (!from || (from as any).nodeName === 'HTML') onPageLeave(); + }; + + document.addEventListener('mouseout', handler); + return () => { + document.removeEventListener('mouseout', handler); + }; }, args); };