참고문서
https://reactnative.dev/docs/appstate.html
AppState · React Native
AppState can tell you if the app is in the foreground or background, and notify you when the state changes.
reactnative.dev
코드
import React, {useRef, useEffect} from 'react';
import {AppState, View} from 'react-native';
const TestContainer = () => {
const appState = useRef(AppState.currentState);
const handleAppStateChange = nextAppState => {
console.log('nextAppState', appState.current, nextAppState);
if (
appState.current.match(/inactive|background/) &&
nextAppState === 'active'
) {
console.log('is foreground!');
// foreground 진입시 실행됨
}
if (
appState.current.match(/inactive|active/) &&
nextAppState === 'background'
) {
console.log('Is background!');
// background 진입시 실행됨
}
appState.current = nextAppState;
};
useEffect(() => {
AppState.addEventListener('change', handleAppStateChange);
return () => {
AppState.removeEventListener('change', handleAppStateChange);
};
}, []);
return <View></View>;
}
export default TestContainer;
'React-native' 카테고리의 다른 글
[ React Native ] 뒤로가기 버튼 비활성화 (0) | 2023.02.20 |
---|---|
[ React Native ] 문자열 자르기 javascript문법 split() (0) | 2023.02.16 |
[ React Native ] 앱 이름 바꾸기 (0) | 2023.01.30 |
[ React Native ] 업데이트 팝업 만들기, 버전관리 (1) | 2023.01.27 |
[ React Native ] 재사용 가능 팝업 ( reusable popup ) 모달 만들기 (0) | 2023.01.26 |