SwitchWithStacks.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * @flow
  3. */
  4. import React from 'react';
  5. import {
  6. ActivityIndicator,
  7. AsyncStorage,
  8. StatusBar,
  9. StyleSheet,
  10. View,
  11. } from 'react-native';
  12. import { createStackNavigator, createSwitchNavigator } from 'react-navigation';
  13. import { Button } from './commonComponents/ButtonWithMargin';
  14. class SignInScreen extends React.Component<any, any> {
  15. static navigationOptions = {
  16. title: 'Please sign in',
  17. };
  18. render() {
  19. return (
  20. <View style={styles.container}>
  21. <Button title="Sign in!" onPress={this._signInAsync} />
  22. <Button
  23. title="Go back to other examples"
  24. onPress={() => this.props.navigation.goBack(null)}
  25. />
  26. <StatusBar barStyle="default" />
  27. </View>
  28. );
  29. }
  30. _signInAsync = async () => {
  31. await AsyncStorage.setItem('userToken', 'abc');
  32. this.props.navigation.navigate('Home');
  33. };
  34. }
  35. class HomeScreen extends React.Component<any, any> {
  36. static navigationOptions = {
  37. title: 'Welcome to the app!',
  38. };
  39. render() {
  40. return (
  41. <View style={styles.container}>
  42. <Button title="Show me more of the app" onPress={this._showMoreApp} />
  43. <Button title="Actually, sign me out :)" onPress={this._signOutAsync} />
  44. <StatusBar barStyle="default" />
  45. </View>
  46. );
  47. }
  48. _showMoreApp = () => {
  49. this.props.navigation.navigate('Other');
  50. };
  51. _signOutAsync = async () => {
  52. await AsyncStorage.clear();
  53. this.props.navigation.navigate('Auth');
  54. };
  55. }
  56. class OtherScreen extends React.Component<any, any> {
  57. static navigationOptions = {
  58. title: 'Lots of features here',
  59. };
  60. render() {
  61. return (
  62. <View style={styles.container}>
  63. <Button title="I'm done, sign me out" onPress={this._signOutAsync} />
  64. <StatusBar barStyle="default" />
  65. </View>
  66. );
  67. }
  68. _signOutAsync = async () => {
  69. await AsyncStorage.clear();
  70. this.props.navigation.navigate('Auth');
  71. };
  72. }
  73. class LoadingScreen extends React.Component<any, any> {
  74. componentDidMount() {
  75. this._bootstrapAsync();
  76. }
  77. _bootstrapAsync = async () => {
  78. const userToken = await AsyncStorage.getItem('userToken');
  79. let initialRouteName = userToken ? 'App' : 'Auth';
  80. this.props.navigation.navigate(initialRouteName);
  81. };
  82. render() {
  83. return (
  84. <View style={styles.container}>
  85. <ActivityIndicator />
  86. <StatusBar barStyle="default" />
  87. </View>
  88. );
  89. }
  90. }
  91. const styles = StyleSheet.create({
  92. container: {
  93. flex: 1,
  94. alignItems: 'center',
  95. justifyContent: 'center',
  96. },
  97. });
  98. const AppStack = createStackNavigator({ Home: HomeScreen, Other: OtherScreen });
  99. const AuthStack = createStackNavigator({ SignIn: SignInScreen });
  100. export default createSwitchNavigator({
  101. Loading: LoadingScreen,
  102. App: AppStack,
  103. Auth: AuthStack,
  104. });