From 25828af37fe28f9ae8d9aac7a10625e6b9e5c0c4 Mon Sep 17 00:00:00 2001 From: Mike Fix Date: Thu, 14 Feb 2019 19:01:46 -0800 Subject: [PATCH] fix up tweet button --- components/TweetButton.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/components/TweetButton.js b/components/TweetButton.js index 23152fc..12d4851 100644 --- a/components/TweetButton.js +++ b/components/TweetButton.js @@ -10,17 +10,27 @@ function useWindowListener(key, fn) { }, []) } -function TweetButton(props) { +function useOnlineListener() { const [online, setOnline] = React.useState(true) - const [onClick, { loading }] = useAsyncCallback(props.onClick) React.useEffect(() => { - setOnline(Boolean(window && window.navigator && window.navigator.onLine)) + setOnline( + typeof navigator !== 'undefined' && typeof navigator.onLine === 'boolean' + ? navigator.onLine + : true + ) }, []) useWindowListener('offline', () => setOnline(false)) useWindowListener('online', () => setOnline(true)) + return online +} + +function TweetButton(props) { + const online = useOnlineListener() + const [onClick, { loading }] = useAsyncCallback(props.onClick) + if (!online) { return null }