forked from robinhuy/react-native-expo-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
77 lines (67 loc) · 1.77 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import React, { useState, useEffect } from "react";
import {
StyleSheet,
SafeAreaView,
ScrollView,
View,
BackHandler,
} from "react-native";
import { Text, ListItem } from "react-native-elements";
import Constants from "expo-constants";
import { EXAMPLE_LIST } from "./example-list";
export default function App() {
const [exampleIndex, setExampleIndex] = useState(null);
// Handle when user press Hardware Back Button
useEffect(() => {
const backAction = () => {
// Go back to Example List
if (exampleIndex !== null) {
setExampleIndex(null);
}
// Exit app if user currently in Example List
else {
BackHandler.exitApp();
}
return true;
};
// https://reactnative.dev/docs/backhandler
const backHandler = BackHandler.addEventListener(
"hardwareBackPress",
backAction
);
return () => backHandler.remove();
}, [exampleIndex]);
if (exampleIndex !== null) return EXAMPLE_LIST[exampleIndex].component;
return (
<SafeAreaView style={styles.container}>
<Text h4 style={styles.heading}>
React Native Expo Examples
</Text>
<ScrollView>
{EXAMPLE_LIST.map((l, i) => (
<ListItem key={i} bottomDivider onPress={() => setExampleIndex(i)}>
<View>
<Text>Level {l.level}</Text>
</View>
<ListItem.Content>
<ListItem.Title style={styles.title}>{l.name}</ListItem.Title>
</ListItem.Content>
</ListItem>
))}
</ScrollView>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: Constants.statusBarHeight,
},
heading: {
textAlign: "center",
padding: 12,
},
title: {
fontWeight: "bold",
},
});