sonner-native
Version:
An opinionated toast component for React Native. A port of @emilkowalski's sonner.
32 lines (27 loc) • 698 B
text/typescript
import React from 'react';
import { AppState } from 'react-native';
export const useAppStateListener = ({
onBackground,
onForeground,
}: {
onBackground: () => void;
onForeground: () => void;
}) => {
const appState = React.useRef(AppState.currentState);
React.useEffect(() => {
const subscription = AppState.addEventListener('change', (nextAppState) => {
if (
appState.current.match(/inactive|background/) &&
nextAppState === 'active'
) {
onForeground();
} else {
onBackground();
}
appState.current = nextAppState;
});
return () => {
subscription.remove();
};
}, [onBackground, onForeground]);
};