Skip to content

Commit d2e222e

Browse files
devofthingsSimek
andauthored
Fixed the Function Component Example (#2019)
* 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. * removed block comment, removed spaces * rephrase and move new comment, add one more sentence, prettier run * language lint fix Co-authored-by: Bartosz Kaszubowski <gosimek@gmail.com>
1 parent 8e4a003 commit d2e222e

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

docs/appstate.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,37 @@ To see the current state, you can check `AppState.currentState`, which will be k
3636
<block class="functional syntax" />
3737

3838
```SnackPlayer name=AppState%20Function%20Component%20Example
39-
import React, { useEffect, useRef } from "react";
39+
import React, { useRef, useState, useEffect } from "react";
4040
import { AppState, StyleSheet, Text, View } from "react-native";
4141
4242
const AppStateExample = () => {
4343
const appState = useRef(AppState.currentState);
44+
const [appStateVisible, setAppStateVisible] = useState(appState.current);
4445
4546
useEffect(() => {
4647
AppState.addEventListener("change", _handleAppStateChange);
4748
4849
return () => {
4950
AppState.removeEventListener("change", _handleAppStateChange);
5051
};
51-
}, [_handleAppStateChange]);
52+
}, []);
5253
5354
const _handleAppStateChange = (nextAppState) => {
54-
if (appState.current.match(/inactive|background/) && nextAppState === "active") {
55+
if (
56+
appState.current.match(/inactive|background/) &&
57+
nextAppState === "active"
58+
) {
5559
console.log("App has come to the foreground!");
5660
}
61+
5762
appState.current = nextAppState;
63+
setAppStateVisible(appState.current);
64+
console.log("AppState", appState.current);
5865
};
5966
6067
return (
6168
<View style={styles.container}>
62-
<Text>Current state is: {appState.current}</Text>
69+
<Text>Current state is: {appStateVisible}</Text>
6370
</View>
6471
);
6572
};
@@ -68,13 +75,15 @@ const styles = StyleSheet.create({
6875
container: {
6976
flex: 1,
7077
justifyContent: "center",
71-
alignItems: "center"
72-
}
78+
alignItems: "center",
79+
},
7380
});
7481
7582
export default AppStateExample;
7683
```
7784

85+
If you don't want to see the AppState update from `active` to `inactive` on iOS you can remove the state variable and use the `appState.current` value.
86+
7887
<block class="classical syntax" />
7988

8089
```SnackPlayer name=AppState%20Class%20Component%20Example
@@ -126,7 +135,7 @@ export default AppStateExample;
126135

127136
<block class="endBlock syntax" />
128137

129-
This example will only ever appear to say "Current state is: active" because the app is only visible to the user when in the `active` state, and the null state will happen only momentarily.
138+
This example will only ever appear to say "Current state is: active" because the app is only visible to the user when in the `active` state, and the null state will happen only momentarily. If you want to experiment with the code we recommend to use your own device instead of embedded preview.
130139

131140
---
132141

0 commit comments

Comments
 (0)