Skip to content

Fixed the Function Component Example #2019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions docs/appstate.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,37 @@ To see the current state, you can check `AppState.currentState`, which will be k
<block class="functional syntax" />

```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);
const [appStateVisible, setAppStateVisible] = useState(appState.current);

useEffect(() => {
AppState.addEventListener("change", _handleAppStateChange);

return () => {
AppState.removeEventListener("change", _handleAppStateChange);
};
}, [_handleAppStateChange]);
}, []);

const _handleAppStateChange = (nextAppState) => {
if (appState.current.match(/inactive|background/) && nextAppState === "active") {
if (
appState.current.match(/inactive|background/) &&
nextAppState === "active"
) {
console.log("App has come to the foreground!");
}

appState.current = nextAppState;
setAppStateVisible(appState.current);
console.log("AppState", appState.current);
};

return (
<View style={styles.container}>
<Text>Current state is: {appState.current}</Text>
<Text>Current state is: {appStateVisible}</Text>
</View>
);
};
Expand All @@ -68,13 +75,15 @@ const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center"
}
alignItems: "center",
},
});

export default AppStateExample;
```

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.

<block class="classical syntax" />

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

<block class="endBlock syntax" />

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.
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.

---

Expand Down