|
1 | 1 | import React, {useEffect} from 'react';
|
2 | 2 | import {AppRegistry, StyleSheet, Text, View, Button} from 'react-native';
|
3 |
| -import {NavigationContainer} from '@react-navigation/native'; |
4 |
| -import {createNativeStackNavigator} from '@react-navigation/native-stack'; |
| 3 | +import { |
| 4 | + createNativeStackNavigator, |
| 5 | + type NativeStackScreenProps, |
| 6 | +} from '@react-navigation/native-stack'; |
5 | 7 | import ReactNativeBrownfield from '@callstack/react-native-brownfield';
|
| 8 | +import {NavigationContainer} from '@react-navigation/native'; |
6 | 9 |
|
| 10 | +const getRandomValue = () => Math.round(Math.random() * 255); |
7 | 11 | const getRandomTheme = () => {
|
8 |
| - const getRandomValue = () => Math.round(Math.random() * 255); |
9 |
| - |
10 | 12 | const primary = [getRandomValue(), getRandomValue(), getRandomValue()];
|
11 |
| - const secondary = [255 - primary[0], 255 - primary[1], 255 - primary[2]]; |
| 13 | + const secondary = [ |
| 14 | + 255 - (primary?.[0] || 0), |
| 15 | + 255 - (primary?.[1] || 0), |
| 16 | + 255 - (primary?.[2] || 0), |
| 17 | + ]; |
12 | 18 |
|
13 | 19 | return {
|
14 | 20 | primary: `rgb(${primary[0]}, ${primary[1]}, ${primary[2]})`,
|
15 | 21 | secondary: `rgb(${secondary[0]}, ${secondary[1]}, ${secondary[2]})`,
|
16 | 22 | };
|
17 | 23 | };
|
18 | 24 |
|
19 |
| -function HomeScreen({navigation}) { |
20 |
| - const colors = navigation.params?.theme || getRandomTheme(); |
| 25 | +type Props = NativeStackScreenProps<RootStackParamList, 'Home'>; |
| 26 | + |
| 27 | +function HomeScreen({navigation, route}: Props) { |
| 28 | + const colors = route.params?.theme || getRandomTheme(); |
21 | 29 |
|
22 | 30 | useEffect(() => {
|
23 |
| - const unsubscribe = navigation.addListener('focus', (e) => { |
| 31 | + const unsubscribe = navigation.addListener('focus', () => { |
24 | 32 | const isFirstRoute = !navigation.canGoBack();
|
25 | 33 | ReactNativeBrownfield.setNativeBackGestureAndButtonEnabled(isFirstRoute);
|
26 | 34 | });
|
27 | 35 | return unsubscribe;
|
28 |
| - }, []); |
| 36 | + }, [navigation]); |
29 | 37 |
|
30 | 38 | return (
|
31 | 39 | <View style={[styles.container, {backgroundColor: colors.primary}]}>
|
@@ -57,8 +65,11 @@ function HomeScreen({navigation}) {
|
57 | 65 | </View>
|
58 | 66 | );
|
59 | 67 | }
|
| 68 | +type RootStackParamList = { |
| 69 | + Home: {theme: {primary: string, secondary: string}}, |
| 70 | +}; |
60 | 71 |
|
61 |
| -const Stack = createNativeStackNavigator(); |
| 72 | +const Stack = createNativeStackNavigator<RootStackParamList>(); |
62 | 73 |
|
63 | 74 | function App() {
|
64 | 75 | return (
|
|
0 commit comments