React Native appState hook is a custom react hook, built to handle iOS or Android appState
in your react component
React hooks is available from react version 16.8.0 and react native version 0.59.0
yarn add react-native-appstate-hook
OR
npm install --save react-native-appstate-hook
import React from 'react';
import { Text, View } from 'react-native';
import useAppState from 'react-native-appstate-hook';
export default function App() {
const { appState } = useAppState({
onChange: (newAppState) => console.warn('App state changed to ', newAppState),
onForeground: () => console.warn('App went to Foreground'),
onBackground: () => console.warn('App went to background'),
});
return (
<View style={{textAlign: 'center', backgroundColor :'white', flex: 1, justifyContent: 'center'}}>
<Text>App State is: {appState}</Text>
</View>
);
}
key | Type | Required | Description |
---|---|---|---|
onChange | Function | No | callback function to be executed once appState is changed |
onForeground | Function | No | callback function to be executed once app go to foreground |
onBackground | Function | No | callback function to be executed once app go to background |
key | Type | Description |
---|---|---|
appState | string | app state it can be one of the following values active , inactive , or background |