1
- import React from 'react' ;
1
+ import React , { useEffect } from 'react' ;
2
2
import { AppRegistry , StyleSheet , Text , View , Button } from 'react-native' ;
3
- import {
4
- createStackNavigator ,
5
- createAppContainer ,
6
- NavigationEvents ,
7
- } from 'react-navigation' ;
3
+ import { NavigationContainer } from '@react-navigation/native' ;
4
+ import { createNativeStackNavigator } from '@react-navigation/native-stack' ;
8
5
import ReactNativeBrownfield from '@callstack/react-native-brownfield' ;
9
6
10
7
const getRandomTheme = ( ) => {
@@ -19,54 +16,58 @@ const getRandomTheme = () => {
19
16
} ;
20
17
} ;
21
18
22
- class HomeScreen extends React . Component {
23
- static navigationOptions = {
24
- header : null ,
25
- } ;
19
+ function HomeScreen ( { navigation} ) {
20
+ const colors = navigation . params ?. theme || getRandomTheme ( ) ;
21
+
22
+ useEffect ( ( ) => {
23
+ const unsubscribe = navigation . addListener ( 'focus' , ( e ) => {
24
+ const isFirstRoute = ! navigation . canGoBack ( ) ;
25
+ ReactNativeBrownfield . setNativeBackGestureAndButtonEnabled ( isFirstRoute ) ;
26
+ } ) ;
27
+ return unsubscribe ;
28
+ } , [ ] ) ;
26
29
27
- render ( ) {
28
- const colors = this . props . navigation . getParam ( 'theme' , getRandomTheme ( ) ) ;
29
- const isFirstRoute = this . props . navigation . isFirstRouteInParent ( ) ;
30
+ return (
31
+ < View style = { [ styles . container , { backgroundColor : colors . primary } ] } >
32
+ < Text style = { [ styles . text , { color : colors . secondary } ] } >
33
+ React Native Screen
34
+ </ Text >
30
35
31
- return (
32
- < >
33
- < NavigationEvents
34
- onWillFocus = { ( ) => {
35
- ReactNativeBrownfield . setNativeBackGestureAndButtonEnabled (
36
- isFirstRoute ,
37
- ) ;
38
- } }
39
- />
40
- < View style = { [ styles . container , { backgroundColor : colors . primary } ] } >
41
- < Text style = { [ styles . text , { color : colors . secondary } ] } >
42
- React Native Screen
43
- </ Text >
36
+ < Button
37
+ onPress = { ( ) => {
38
+ navigation . push ( 'Home' , {
39
+ theme : getRandomTheme ( ) ,
40
+ } ) ;
41
+ } }
42
+ color = { colors . secondary }
43
+ title = "Push next screen"
44
+ />
45
+
46
+ < Button
47
+ onPress = { ( ) => {
48
+ if ( navigation . canGoBack ( ) ) {
49
+ navigation . goBack ( ) ;
50
+ } else {
51
+ ReactNativeBrownfield . popToNative ( true ) ;
52
+ }
53
+ } }
54
+ color = { colors . secondary }
55
+ title = "Go back"
56
+ />
57
+ </ View >
58
+ ) ;
59
+ }
44
60
45
- < Button
46
- onPress = { ( ) => {
47
- this . props . navigation . push ( 'Home' , {
48
- theme : getRandomTheme ( ) ,
49
- } ) ;
50
- } }
51
- color = { colors . secondary }
52
- title = "Push next screen"
53
- />
61
+ const Stack = createNativeStackNavigator ( ) ;
54
62
55
- < Button
56
- onPress = { ( ) => {
57
- if ( isFirstRoute ) {
58
- ReactNativeBrownfield . popToNative ( true ) ;
59
- } else {
60
- this . props . navigation . goBack ( ) ;
61
- }
62
- } }
63
- color = { colors . secondary }
64
- title = "Go back"
65
- />
66
- </ View >
67
- </ >
68
- ) ;
69
- }
63
+ function App ( ) {
64
+ return (
65
+ < NavigationContainer >
66
+ < Stack . Navigator >
67
+ < Stack . Screen name = "Home" component = { HomeScreen } />
68
+ </ Stack . Navigator >
69
+ </ NavigationContainer >
70
+ ) ;
70
71
}
71
72
72
73
const styles = StyleSheet . create ( {
@@ -82,12 +83,4 @@ const styles = StyleSheet.create({
82
83
} ,
83
84
} ) ;
84
85
85
- const AppNavigator = createStackNavigator ( {
86
- Home : {
87
- screen : HomeScreen ,
88
- } ,
89
- } ) ;
90
-
91
- const App = createAppContainer ( AppNavigator ) ;
92
-
93
86
AppRegistry . registerComponent ( 'ReactNative' , ( ) => App ) ;
0 commit comments