Skip to content

Instantly share code, notes, and snippets.

@linux08
Created September 7, 2018 23:42
Show Gist options
  • Select an option

  • Save linux08/f3751899678517f8529504ad24b86977 to your computer and use it in GitHub Desktop.

Select an option

Save linux08/f3751899678517f8529504ad24b86977 to your computer and use it in GitHub Desktop.
import React from 'react';
import {
ActivityIndicator,
AsyncStorage,
StatusBar,
StyleSheet,
View,
} from 'react-native';
import { MyContext } from '../Provider';
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});
class AuthLoadingScreen extends React.Component {
static navigationOptions = {
header: null,
};
constructor() {
super();
}
componentDidMount() {
this._bootstrapAsync();
}
// Fetch the token from storage then navigate to our appropriate place
_bootstrapAsync = async () => {
this.props.context.getToken()
.then((userToken) => {
// This will switch to the App screen or Auth screen and this loading
// screen will be unmounted and thrown away.
this.props.navigation.navigate(userToken ? 'App' : 'Auth');
})
.catch(error => {
this.setState({ error });
})
};
render() {
return (
<View style={styles.container}>
<MyContext.Consumer>
{context => ((
<View>
<ActivityIndicator />
<StatusBar barStyle="default" />
</View>
))}
</MyContext.Consumer>
</View>
);
}
};
const ForwardRef = React.forwardRef((props, ref) => (
<MyContext.Consumer>
{context => <AuthLoadingScreen context={context} {...props} />}
</MyContext.Consumer>
));
export default ({ navigation }) => (
<View style={styles.container}>
<ForwardRef
navigation={navigation}
/>
</View>
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment