-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
153 lines (132 loc) · 3.63 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import 'react-native-gesture-handler';
import React, {useContext, useReducer, useEffect} from 'react';
import {Button, Pressable, SafeAreaView, Text, View} from 'react-native';
import {NavigationContainer} from '@react-navigation/native';
import Bugsnag from '@bugsnag/react-native';
import BugsnagPluginReact from '@bugsnag/plugin-react';
import BugsnagPluginReactNavigation from '@bugsnag/plugin-react-navigation';
import {createBottomTabNavigator} from '@react-navigation/bottom-tabs';
import {createNativeStackNavigator} from '@react-navigation/native-stack';
Bugsnag.start({
plugins: [new BugsnagPluginReact(), new BugsnagPluginReactNavigation()],
});
const ErrorBoundary = Bugsnag.getPlugin('react').createErrorBoundary(React);
const ErrorView = ({clearError}) => {
return (
<View>
<Text>
Something unexpected happened. We're sorry about that, we've let our
engineers know. You can click below to start over
</Text>
<Button title="Reset" onPress={clearError} />
</View>
);
};
const UserContext = React.createContext();
const actions = {SET_FOO: 'SET_FOO'};
const reducer = (state, action) => {
return {...state, foo: action.foo};
};
const UserProvider = ({children}) => {
const [state, dispatch] = useReducer(reducer, {foo: [], loggedIn: true});
const value = {
loggedIn: state.loggedIn,
foo: state.foo,
setFoo: newFoo =>
dispatch({
type: actions.SET_FOO,
foo: newFoo,
}),
};
return <UserContext.Provider value={value}>{children}</UserContext.Provider>;
};
const ScreenA = () => {
const {foo} = useContext(UserContext);
return (
<SafeAreaView>
<Text>Screen A</Text>
<Text>foo == {foo.toString()}</Text>
</SafeAreaView>
);
};
const ScreenC = () => {
const {foo, setFoo} = useContext(UserContext);
useEffect(() => {
setTimeout(() => {
console.log('screen c effect');
setFoo([50, 80]);
}, 1500);
}, []);
return (
<SafeAreaView>
<Text>Screen C</Text>
<Text>foo == {foo.toString()}</Text>
</SafeAreaView>
);
};
const ScreenB = ({navigation}) => {
return (
<SafeAreaView>
<Text style={{marginVertical: 30}}>Screen B</Text>
<Pressable onPress={() => navigation.navigate('Screen C')}>
<Text style={{color: 'red'}}>Link to Screen C</Text>
</Pressable>
</SafeAreaView>
);
};
const TabIndex = () => {
const Tab = createBottomTabNavigator();
return (
<Tab.Navigator>
<Tab.Screen name="Screen A" component={ScreenA} />
<Tab.Screen name="Screen B" component={ScreenB} />
</Tab.Navigator>
);
};
const LoginScreen = () => {
return (
<View>
<Text>Log in here</Text>
</View>
);
};
const AuthStateScreen = () => {
const {loggedIn} = useContext(UserContext);
const {createNavigationContainer} = Bugsnag.getPlugin('reactNavigation');
const BugsnagNavigationContainer =
createNavigationContainer(NavigationContainer);
const Stack = createNativeStackNavigator();
if (loggedIn) {
return (
<BugsnagNavigationContainer>
<Stack.Navigator>
<Stack.Screen name="TabIndex" component={TabIndex} />
<Stack.Screen name="Screen C" component={ScreenC} />
</Stack.Navigator>
</BugsnagNavigationContainer>
);
} else {
return <LoginScreen />;
}
};
const App = () => {
return (
<UserProvider>
<AuthStateScreen />
</UserProvider>
);
};
export default () => {
return (
<ErrorBoundary FallbackComponent={ErrorView}>
<App />
</ErrorBoundary>
);
};