react-native-useappstate
Version:
React Native AppState hook
32 lines (31 loc) • 1.27 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("react");
const react_native_1 = require("react-native");
const index = () => {
const [appState, setAppState] = (0, react_1.useState)(null);
const onAppStateChange = (state) => {
setAppState(state);
};
(0, react_1.useEffect)(() => {
const listener = react_native_1.AppState.addEventListener("change", onAppStateChange);
return () => {
try {
// @ts-ignore
if (listener instanceof Object) { //Use new way of removing event listener
// @ts-ignore
listener === null || listener === void 0 ? void 0 : listener.remove();
}
else { //Use deprecated way of removing event listener
// @ts-ignore
react_native_1.AppState === null || react_native_1.AppState === void 0 ? void 0 : react_native_1.AppState.removeEventListener("change", onAppStateChange);
}
}
catch (_a) {
console.warn("react-native-useappstate problem with removing listener");
}
};
}, []);
return appState;
};
exports.default = index;
;