From 78ab6bf1f02b3243f0b28102cdb9055f26fa2ef4 Mon Sep 17 00:00:00 2001 From: akimabs Date: Tue, 2 Nov 2021 18:08:09 +0700 Subject: [PATCH] fix undefined of an Object appState.remove --- dist/index.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/dist/index.js b/dist/index.js index 1200bee..550235f 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,5 +1,5 @@ -import { useState, useEffect } from 'react'; -import { AppState } from 'react-native'; +import { useState, useEffect } from "react"; +import { AppState } from "react-native"; export default function useAppState(settings) { const { onChange, onForeground, onBackground } = settings || {}; @@ -7,22 +7,25 @@ export default function useAppState(settings) { useEffect(() => { function handleAppStateChange(nextAppState) { - if (nextAppState === 'active' && appState !== 'active') { + if (nextAppState === "active" && appState !== "active") { isValidFunction(onForeground) && onForeground(); - } else if (appState === 'active' && nextAppState.match(/inactive|background/)) { + } else if ( + appState === "active" && + nextAppState.match(/inactive|background/) + ) { isValidFunction(onBackground) && onBackground(); } setAppState(nextAppState); isValidFunction(onChange) && onChange(nextAppState); } - const appState = AppState.addEventListener('change', handleAppStateChange); + const appState = AppState.addEventListener("change", handleAppStateChange); - return () => appState.remove(); + return () => appState?.remove(); }, [onChange, onForeground, onBackground, appState]); // settings validation function isValidFunction(func) { - return func && typeof func === 'function'; + return func && typeof func === "function"; } return { appState }; }