omnipay-savings-sdk
Version:
Omnipay Savings SDK
34 lines (33 loc) • 1.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("react");
const react_native_1 = require("react-native");
function useAppState(settings) {
const { onChange, onForeground, onBackground } = settings || {};
const [appState, setAppState] = (0, react_1.useState)(react_native_1.AppState.currentState);
(0, react_1.useEffect)(() => {
function handleAppStateChange(nextAppState) {
if (nextAppState === 'active' && appState !== 'active') {
if (onForeground) {
onForeground();
}
}
else if (appState === 'active' &&
nextAppState.match(/inactive|background/)) {
if (onBackground) {
onBackground();
}
}
setAppState(nextAppState);
if (onChange) {
onChange(nextAppState);
}
}
const appStateEvent = react_native_1.AppState.addEventListener('change', handleAppStateChange);
return () => {
appStateEvent.remove();
};
}, [onChange, onForeground, onBackground, appState]);
return { appState };
}
exports.default = useAppState;