amrlabib / react-native-appstate-hook Goto Github PK
View Code? Open in Web Editor NEWReact Native appSate hook
Home Page: https://www.npmjs.com/package/react-native-appstate-hook
License: MIT License
React Native appSate hook
Home Page: https://www.npmjs.com/package/react-native-appstate-hook
License: MIT License
Thanks for this package =)
But there is warning a [email protected]" has incorrect peer dependency "[email protected]"
, it's not a big deal, but is it possible to update on "react-native": "0.61.0"
?
While onChange
and onForeground
seem to work perfectly, onBackground
does not seem to fire - at least for iOS 15.2 running on an iPhone 13 simulator.
node_modules/react-native-appstate-hook/dist/index.js
`import { useState, useEffect } from 'react';
import { AppState } from 'react-native';
export default function useAppState(settings) {
const { onChange, onForeground, onBackground } = settings || {};
const [appState, setAppState] = useState(AppState.currentState);
useEffect(() => {
function handleAppStateChange(nextAppState) {
if (nextAppState === 'active' && appState !== 'active') {
isValidFunction(onForeground) && onForeground();
} else if (appState === 'active' && nextAppState.match(/inactive|background/)) {
isValidFunction(onBackground) && onBackground();
}
setAppState(nextAppState);
isValidFunction(onChange) && onChange(nextAppState);
}
const appState = AppState.addEventListener('change', handleAppStateChange);
// removing -> return appState.remove();
return;
}, [onChange, onForeground, onBackground, appState]);
// settings validation
function isValidFunction(func) {
return func && typeof func === 'function';
}
return { appState };
}`
Interim solution!
Hello !
This first issue is clearly not an issue.
I would like to thank you for this very useful hook.
Keep going ! 🚀
Note: I could make a PR for this if this package is still being maintained and is open to PRs, but seeing as the directory structure only includes a dist
folder, I assume this package only includes the JS-compiled package.
I was looking to wrap the RN AppState in my own custom hook and came across this package. While there's some functionality I still wish to add myself, I noticed that it looked like the hook has appState
as a dependency, which creates a lot of excess listener creations and cleanups.
By modifying the handleAppStateChange
function to:
function handleAppStateChange(nextAppState) {
setAppState((prevAppState) => {
if (nextAppState === 'active' && prevAppState !== 'active')
isValidFunction(onForeground) && onForeground();
else if (prevAppState === 'active' && nextAppState.match(/inactive|background/))
isValidFunction(onBackground) && onBackground();
});
isValidFunction(onChange) && onChange(nextAppState);
}
and make the dependency list: [onChange, onForeground, onBackground]
so that changes to the appState
do not trigger the creation of a new AppState listener after the previous listener is cleaned up.
This change isn't strictly necessary, since the hook's appState
and callback function calls remain unaffected by the number of times the change listeners are added/removed, but it was just something I noticed while testing the two methods.
Testing on a Galaxy J7 with Expo 36 release build and the following code:
useAppState({
onForeground: () => {
alert('on foreground. this is a test.');
},
onBackground: () => {
alert('on background. this is a test.');
},
});
On iOS this code works correctly. onForeground and onBackground are never called unless the app is minimized which changes the appState.
On Android, this code does not work the same way. First off, onForeground gets called on screen mount when it shouldn't. this isn't an appstate change, this is just when first running the hook. Then subsequently onForeground gets called on future app state changes. This behavior occurs only on device release build -- it does not occur on emulator or on a USB cable connected device.
npm ERR! Found: [email protected]
npm ERR! node_modules/react-native
npm ERR! react-native@"0.62.2" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react-native@"^0.59.0" from [email protected]
npm ERR! node_modules/react-native-appstate-hook
npm ERR! react-native-appstate-hook@"*" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
Regarding 2.5.1, we found that your app uses or references the com.apple.springboard.lockcomplete
API.
To resolve this issue, it would be appropriate to revise your binary and remove any references to the non-public or deprecated APIs that we have identified.
We look forward to reviewing your resubmitted app.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.