-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathApp.tsx
More file actions
95 lines (84 loc) · 2.45 KB
/
Copy pathApp.tsx
File metadata and controls
95 lines (84 loc) · 2.45 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import React, { useEffect } from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
import {
createNativeStackNavigator,
type NativeStackScreenProps,
} from '@react-navigation/native-stack';
import ReactNativeBrownfield from '@callstack/react-native-brownfield';
import { NavigationContainer } from '@react-navigation/native';
const getRandomValue = () => Math.round(Math.random() * 255);
const getRandomTheme = () => {
const primary = [getRandomValue(), getRandomValue(), getRandomValue()];
const secondary = [
255 - (primary?.[0] || 0),
255 - (primary?.[1] || 0),
255 - (primary?.[2] || 0),
];
return {
primary: `rgb(${primary[0]}, ${primary[1]}, ${primary[2]})`,
secondary: `rgb(${secondary[0]}, ${secondary[1]}, ${secondary[2]})`,
};
};
type Props = NativeStackScreenProps<RootStackParamList, 'Home'>;
function HomeScreen({ navigation, route }: Props) {
const colors = route.params?.theme || getRandomTheme();
useEffect(() => {
const unsubscribe = navigation.addListener('focus', () => {
const isFirstRoute = !navigation.canGoBack();
ReactNativeBrownfield.setNativeBackGestureAndButtonEnabled(isFirstRoute);
});
return unsubscribe;
}, [navigation]);
return (
<View style={[styles.container, { backgroundColor: colors.primary }]}>
<Text style={[styles.text, { color: colors.secondary }]}>
React Native Screen
</Text>
<Button
onPress={() => {
navigation.push('Home', {
theme: getRandomTheme(),
});
}}
color={colors.secondary}
title="Push next screen"
/>
<Button
onPress={() => {
if (navigation.canGoBack()) {
navigation.goBack();
} else {
ReactNativeBrownfield.popToNative(true);
}
}}
color={colors.secondary}
title="Go back"
/>
</View>
);
}
type RootStackParamList = {
Home: { theme: { primary: string; secondary: string } };
};
const Stack = createNativeStackNavigator<RootStackParamList>();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
text: {
fontSize: 30,
fontWeight: 'bold',
margin: 10,
},
});