From 8344ec8ed41280f413f5f01d6db9fb4b04daf89d Mon Sep 17 00:00:00 2001 From: Chris Winter Date: Thu, 2 Jul 2020 10:01:57 +0200 Subject: [PATCH 1/4] Fixed the Function Component Example We experienced that the documentation was faulty so after we at @enpit found a fix, we wanted to share it with you. The old trigger `appState.current.match(/inactive|background/) && nextAppState === "active"` would never work because the useState hook doesn't update the state fast enough for the function to work. The solution is to use useRef instead and optional (to display the inactive status under iOS) also use a `useState` for cosmetics. --- docs/appstate.md | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/docs/appstate.md b/docs/appstate.md index c22729798d8..898e8a0fef5 100644 --- a/docs/appstate.md +++ b/docs/appstate.md @@ -36,30 +36,38 @@ To see the current state, you can check `AppState.currentState`, which will be k ```SnackPlayer name=AppState%20Function%20Component%20Example -import React, { useEffect, useRef } from "react"; +import React, { useRef, useState, useEffect } from "react"; import { AppState, StyleSheet, Text, View } from "react-native"; const AppStateExample = () => { const appState = useRef(AppState.currentState); - - useEffect(() => { - AppState.addEventListener("change", _handleAppStateChange); - - return () => { - AppState.removeEventListener("change", _handleAppStateChange); + /* + You can remove line 7 & 24 if you don't want to see the AppState update from active to inactive (iOS) + You then must alter line 30 to something like appState.current + */ + const [appStateVisible, setAppStateVisible] = useState(appState.current); + + useEffect(() => { + AppState.addEventListener('change', _handleAppStateChange); + + return () => { + AppState.removeEventListener('change', _handleAppStateChange); + }; + }, []); + + const _handleAppStateChange = (nextAppState) => { + if (appState.current.match(/inactive|background/) && nextAppState === 'active') { + console.log("App has come to the foreground!"); + } + + appState.current = nextAppState; + console.log("AppState", appState.current); + setAppStateVisible(appState.current); }; - }, [_handleAppStateChange]); - - const _handleAppStateChange = (nextAppState) => { - if (appState.current.match(/inactive|background/) && nextAppState === "active") { - console.log("App has come to the foreground!"); - } - appState.current = nextAppState; - }; return ( - Current state is: {appState.current} + Current state is: {appStateVisible} ); }; From b9c97d809870b723f26ee9b8ddba70e770f23f83 Mon Sep 17 00:00:00 2001 From: Chris Winter Date: Mon, 6 Jul 2020 15:53:17 +0200 Subject: [PATCH 2/4] removed block comment, removed spaces removed the block comment from code and added it as a descriptive text above. Some spaces were removed because the code was too indented. --- docs/appstate.md | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/docs/appstate.md b/docs/appstate.md index 898e8a0fef5..b8d76b58aff 100644 --- a/docs/appstate.md +++ b/docs/appstate.md @@ -22,6 +22,9 @@ For more information, see [Apple's documentation](https://developer.apple.com/do To see the current state, you can check `AppState.currentState`, which will be kept up-to-date. However, `currentState` will be null at launch while `AppState` retrieves it over the bridge. +You can remove line 7 & 24 in the functional component example if you don't want to see the AppState update from active to inactive (iOS). +After that you must alter line 30 to something like `appState.current`. +