Splash screen should be initialized before rendering home screen - react-native

...
I am trying to remove Drawer Navigation and use bottom navigation instead, but I am not able to display the splash screen and then the home screen. Please help me as I am totally new to React Native
...
...
navigation code
...
import React from "react";
import { createStackNavigator, createAppContainer, createBottomTabNavigator } from "react-navigation";
import DoctorHome from "../containers/Home/DoctorHome/DoctorHome";
import Appointments from "../containers/DoctorFlow/Appointments/Appointments";
import EditProfile from "../containers/DoctorFlow/EditProfile/EditProfile";
import ViewClinic from "../containers/DoctorFlow/ViewClinic/ViewClinic";
import AddClinic from "../containers/DoctorFlow/AddClinic/AddClinic";
import Profile from "../containers/DoctorFlow/Profile/Profile";
import Proffession from "../containers/DoctorFlow/Profile/Proffession";
import {
View,
Image,
Touchable,
TouchableHighlight,
TouchableNativeFeedback,
Platform
} from "react-native";
const HomeStack = createStackNavigator ({
Home: DoctorHome,
Appointments: Appointments,
EditProfile: EditProfile
});
const ClinicStack = createStackNavigator ({
Clinic: ViewClinic,
AddClinic: AddClinic
});
const ProfileStack = createStackNavigator ({
Profile: Profile,
EditProfile: EditProfile,
Proffession: Proffession
});
const MainNavigator = createBottomTabNavigator({
Home: HomeStack,
Clinic: ClinicStack,
Profile: ProfileStack
});
export const AppNavigator = createAppContainer(MainNavigator);
...
splash screen code
...
import React, { Component } from "react";
import { AsyncStorage, Image, StyleSheet, Text, View } from "react-native";
import { connect } from "react-redux";
import TimerMixin from "react-timer-mixin";
import { StackActions, NavigationActions } from "react-navigation";
class Splash extends Component {
constructor(props) {
super(props);
this.state = {
user: null
};
}
componentWillMount() {}
componentDidMount() {
this.getUser();
TimerMixin.setTimeout(() => {
if (this.state.user) {
console.log(this.state.user.user.userType);
if (this.state.user.user.userType == "DOCTOR") {
if (this.state.user.user.isProfileCompleted == false) {
this.props.navigation.dispatch(
StackActions.reset({
index: 0,
actions: [
NavigationActions.navigate({ routeName: "EditDoctorProfile" })
]
})
);
} else {
this.props.navigation.dispatch(
StackActions.reset({
index: 0,
actions: [
NavigationActions.navigate({ routeName: "DoctorHomeMenu" })
]
})
);
}
}
} else {
this.props.navigation.dispatch(
StackActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: "Login" })]
})
);
}
}, 1000);
}
async getUser() {
await AsyncStorage.getItem("user").then(user =>
this.setState({ user: JSON.parse(user) })
);
}
render() {
return (
<View
style={{
justifyContent: "center",
alignItems: "center",
flex: 1,
backgroundColor: "#fff"
}}
>
<Image
style={{ width: 200, height: 40 }}
source={require("../Images/logo/logo.png")}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
justifyContent: "center",
alignItems: "center",
backgroundColor: "#F5FCFF",
padding: 16
},
welcome: {
fontSize: 20,
textAlign: "center",
margin: 10
},
instructions: {
textAlign: "center",
color: "#333333",
marginBottom: 5
}
});
const mapDispatchToProps = dispatch => ({
Login: () =>
dispatch(
NavigationActions.navigate({
routeName: "Login"
})
)
});
export default connect(
null,
mapDispatchToProps
)(Splash);
...
App.js (entry point)
...
import React from "react";
import { View, StatusBar } from "react-native";
import { Provider } from "react-redux";
import { AppNavigator } from "../Navigation/RootNavigation";
import configureStore from "../store/ConfigureStore";
import color from "../ui/color";
const store = configureStore();
export default class App extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<View
style={{
flex: 1,
backgroundColor: color.primary
}}
>
<StatusBar
backgroundColor={"#000"}
barStyle="light-content"
hidden={false}
/>
<Provider store={store}>
<AppNavigator />
</Provider>
</View>
);
}
}
...
I want to display the logo first, and then I want a login/signup screen. Once the user logs in, he can access Home and inner screens.
But right now, I am directly getting Home screen as my opening screen. Please help me resolve this issue.
...

You'd probably want to make the login stack the default route:
const MainNavigator = createBottomTabNavigator({
Home: HomeStack,
Clinic: ClinicStack,
Profile: ProfileStack,
Login: LoginStack
}, {initialRouteName: 'Login'});
You probably also want to add the splash screen make it the default route
const LoginStack = createStackNavigator ({
Login: Login,
SignUp: SignUp,
Splash: Splash
}, {initialRouteName: 'Splash')

Related

error adding a splash to my expo react native app

I have my next splash, my idea is to be able to show my app logo when opening the application and then go to the start of the application, it works fine:
import React from 'react';
import { StyleSheet, Image, View, Text } from 'react-native';
import { StackActions, NavigationActions } from 'react-navigation';
export default class Splash extends React.Component {
goToScreen(routeName){
const resetAction = StackActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: routeName })],
});
this.props.navigation.dispatch(resetAction);
}
/*goToScreen(routeName){
this.props.navigation.navigate(routeName)
}*/
componentDidMount(){
setTimeout( () => {
this.goToScreen('Home')
}, 2000, this)
}
render(){
return (
<View style={styles.container}>
<Image source={{uri: 'https://i.imgur.com/r0jUwOD.png'}} style={{width: 250, height: 250}} />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#33B747',
alignItems: 'center',
justifyContent: 'center'
},
});
The problem is the location of the splash, since in this way if it shows me, but it creates a lower button called Splash, on the other hand the splash when loading is shown in conjunction with the lower buttons and that should not happen, the splash must display only in full screen and not create a button.
AppNavigator.js
import React from 'react';
import { createStackNavigator } from 'react-navigation-stack';
import { createBottomTabNavigator } from 'react-navigation-tabs';
import { createAppContainer } from 'react-navigation';
import { MaterialIcons, MaterialCommunityIcons } from '#expo/vector-icons';
import Splash from '../screens/Splash';
import NewsListScreen from '../screens/NewsListScreen';
import NewsItemScreen from '../screens/NewsItemScreen';
const StackNavigator = createStackNavigator({
Splash: {
screen: Splash,
navigationOptions: {
headerShown: false,
}
},
News: {
screen: NewsListScreen
},
Root: {
screen: BottomTabNavigator,
},
NewsItem: {
screen: NewsItemScreen,
navigationOptions: {
headerTitle: 'News Item'
}
},
},{
initialRouteName: 'Splash'
});
const BottomTabNavigator = createBottomTabNavigator({
Home: {
screen: StackNavigator,
navigationOptions: {
tabBarIcon: () => <MaterialIcons name="home" size={24} />
}
},
News: {
screen: StackNavigator,
navigationOptions: {
tabBarIcon: () => <MaterialCommunityIcons name="newspaper-variant-outline" size={24} />
}
}
})
export default createAppContainer(BottomTabNavigator);
App.js
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { AppLoading } from 'expo';
import { Provider } from 'react-redux';
import AppNavigator from './app/resources/navigation/AppNavigator';
import ReduxStore from './app/resources/redux/ReduxStore';
export default function App() {
return (
<Provider store={ReduxStore}>
<AppNavigator />
</Provider>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
Attached capture of the error upload in imgur capture 1 and capture 2
The recommended way to add a splash screen to an expo managed app can be found here.
I've managed to create an expo project and achieve the exact same behavior that you've looking for.
I've used react-navigation 4.x.x
This is how it looks and you can find the full code here
Here is the code I've used.
Splash.js
import React, { Component } from "react";
import { Text, SafeAreaView, View, StyleSheet } from "react-native";
import { StackActions, NavigationActions } from "react-navigation";
export class Splash extends Component {
goToScreen(routeName) {
const resetAction = StackActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: routeName })],
});
this.props.navigation.dispatch(resetAction);
}
componentDidMount() {
setTimeout(
() => {
this.goToScreen("Root");
},
2000,
this
);
}
render() {
return (
<SafeAreaView style={styles.mainContainer}>
<Text style={styles.text}> Splash Screen </Text>
</SafeAreaView>
);
}
}
const styles = StyleSheet.create({
mainContainer: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#efef21",
},
text: {
fontSize: 22,
},
});
export default Splash;
Navigator.js
import { createStackNavigator } from "react-navigation-stack";
import { createAppContainer } from "react-navigation";
import Home from "./Home";
import Splash from "./Splash";
import TabNavigator from "./BottomTabNavigator";
const AppNavigator = createStackNavigator(
{
Splash: {
screen: Splash,
navigationOptions: {
headerShown: false,
},
},
Root: {
screen: TabNavigator,
navigationOptions: {
headerShown: false,
},
},
},
{
initialRouteName: "Splash",
}
);
export default createAppContainer(AppNavigator);
BottomTabNavigator.js
import { createAppContainer } from "react-navigation";
import { createBottomTabNavigator } from "react-navigation-tabs";
import Home from "./Home";
import News from "./News";
const TabNavigator = createBottomTabNavigator({
Home: {
screen: Home,
navigationOptions: {
title: "Home",
},
},
News: {
screen: News,
navigationOptions: {
title: "News",
},
},
});
export default createAppContainer(TabNavigator);
Also, I've achieved the same behavior using react-navigation 5.x.x
You can find my code here
Edit 01
If you want to have the headers, You need to use a Stack Navigator for each tab.
Here is the updated code of BottomTabNAvigator.js
import { createAppContainer } from "react-navigation";
import { createBottomTabNavigator } from "react-navigation-tabs";
import { createStackNavigator } from "react-navigation-stack";
import Home from "./Home";
import News from "./News";
const HomeStack = createStackNavigator({
Home: Home,
});
const NewsStack = createStackNavigator({
News: News,
});
const TabNavigator = createBottomTabNavigator({
Home: {
screen: HomeStack,
navigationOptions: {
headerTitle: "Home",
title: "Home",
},
},
News: {
screen: NewsStack,
navigationOptions: {
title: "News",
},
},
});
export default createAppContainer(TabNavigator);
You have used Splash: {screen: Splash}, in your BottomTabNavigator so obviously it will add splash as on bottom navigator.
Remove Splash: {screen: Splash}, from BottomTabNavigator and paste it on StackNavigator and set splash as initialRouteName
const StackNavigator = createStackNavigator({
News: {
screen: NewsListScreen
},
Splash: {screen: Splash},
NewsItem: {
screen: NewsItemScreen,
navigationOptions: {
headerTitle: 'News Item'
}
},
},
{
initialRouteName : 'Splash'
});
you have to move splash screen to StackNavigator and add BottomTabNavigator into StackNavigator as a screen like this
import React from 'react';
import { createStackNavigator } from 'react-navigation-stack';
import { createBottomTabNavigator } from 'react-navigation-tabs';
import { createAppContainer } from 'react-navigation';
import { MaterialIcons } from '#expo/vector-icons';
import Splash from '../screens/Splash';
import NewsListScreen from '../screens/NewsListScreen';
import NewsItemScreen from '../screens/NewsItemScreen';
const StackNavigator = createStackNavigator({
Splash: {
screen: Splash
},
Home: {
screen: BottomTabNavigator
},
News: {
screen: NewsListScreen
},
NewsItem: {
screen: NewsItemScreen,
navigationOptions: {
headerTitle: 'News Item'
}
}
},
{
initialRouteName: "Splash",
});
const BottomTabNavigator = createBottomTabNavigator({
Home: {
screen: StackNavigator,
navigationOptions: {
tabBarIcon: () => <MaterialIcons name="home" size={24} />
}
}
})
export default createAppContainer(StackNavigator);

Passing information from one screen to another (using function component)

Passing information from one screen (CategoriesScreen) to another (MealDetailScreen). Both Screens are function component. The error occurs when the onDateChange function is executed. The props.navigation.navigate... line is the issue.
From:
import React from 'react';
import {
StyleSheet,
Text,
View
} from 'react-native';
import CalendarPicker from 'react-native-calendar-picker';
const CategoriesScreen = props => {
return (
<View style={styles.container}>
<CalendarPicker
onDateChange={onDateChange}
/>
</View>
);
};
const onDateChange = (onDateChange) => {
console.log(props);
props.navigation.navigate({
routeName: 'MealDetail',
params: { dateId: onDateChange }
});
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#FFFFFF',
marginTop: 10,
},
});
export default CategoriesScreen;
To:
import React from 'react';
import { View, Text, Button, StyleSheet } from 'react-native';
import { HeaderButtons, Item } from 'react-navigation-header-buttons';
import { MEALS } from '../data/dummy-data';
import HeaderButton from '../components/HeaderButton';
const MealDetailScreen = props => {
const date = props.navigation.getParam('dateId');
return (
<Text> {date} </Text>
);
};
const styles = StyleSheet.create({
screen: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}
});
export default MealDetailScreen;
Navigator:
import { Platform } from 'react-native';
import { createStackNavigator, createAppContainer } from 'react-navigation';
import CategoriesScreen from '../screens/CategoriesScreen';
import CategoryMealsScreen from '../screens/CategoryMealsScreen';
import MealDetailScreen from '../screens/MealDetailScreen';
import Colors from '../constants/Colors';
const MealsNavigator = createStackNavigator(
{
Categories: {
screen: CategoriesScreen
},
CategoryMeals: {
screen: CategoryMealsScreen
},
MealDetail: MealDetailScreen
},
{
// initialRouteName: 'Categories',
defaultNavigationOptions: {
headerStyle: {
backgroundColor: Platform.OS === 'android' ? Colors.primaryColor : ''
},
headerTintColor:
Platform.OS === 'android' ? 'white' : Colors.primaryColor,
headerTitle: 'Quick Time'
}
}
);
export default createAppContainer(MealsNavigator);
When the onDateChange function gets executed, the following error appears:
Error Message
just do this put on onDateChange function inside scope of component
const CategoriesScreen = props => {
const onDateChange = (onDateChange) => {
console.log(props);
props.navigation.navigate({
routeName: 'MealDetail',
params: { dateId: onDateChange }
});
};
return (
<View style={styles.container}>
<CalendarPicker
onDateChange={onDateChange}
/>
</View>
);
};

React native, routing without a nav menu item

I have a drawer and bottom tab nav system on my app which works perfectly, but I"ve come to a point where I have modules where I want a button click to take the user to a page for data entry but I don't want that page on any nav bars, I just need to route to it on button press.
So in App.js i have my existing nav system:
import React, { Component } from 'react';
import { AppRegistry, StyleSheet, Text, View, Image } from 'react-native';
import Splash from './Splash';
import Login from './src/components/Login/Login';
import Dashboard from './src/components/Dashboard/Dashboard';
import Trends from './src/components/Trends/Trends';
import EnterWeight from './src/components/Dashboard/EnterWeight';
import Profile from './src/Profile';
import {createAppContainer, createSwitchNavigator} from 'react-navigation';
import {createStackNavigator} from 'react-navigation-stack';
import { createMaterialBottomTabNavigator } from 'react-navigation-material-bottom-tabs';
import { createDrawerNavigator } from 'react-navigation-drawer';
import Icon from '#expo/vector-icons/Ionicons';
import * as SQLite from 'expo-sqlite';
const db = SQLite.openDatabase('testDatabase');
const DashboardTabNavigator = createMaterialBottomTabNavigator({
Dashboard: {screen:Dashboard},
Trends: {screen:Trends},
//Profile: {screen:Profile},
EnterWeight: {screen:EnterWeight}
},
{
barStyle: { backgroundColor: '#000'},
},
{
navigationOptions:({navigation})=>{
const {routeName} = navigation.state.routes[navigation.state.index]
return{
headerTitle:routeName
}
}
}
);
const DashboardStackNavigator = createStackNavigator({
DashboardTabNavigator: DashboardTabNavigator
},
{
defaultNavigationOptions:({navigation}) => {
return {
headerLeft: (<Icon style={{paddingLeft:10}} onPress={()=>navigation.openDrawer()} name="md-menu" size={30} />),
headerRight: (<Image style={styles.crossLogo} source={require('./src/images/LOGO-cross_only.png')}/>)
}
}
});
const AppDrawerNavigator = createDrawerNavigator({
Dashboard:{screen:DashboardStackNavigator},
Logout:{screen:Login}
});
const AppSwitchNavigator = createSwitchNavigator({
Login:{screen: Login},
Dashboard:{screen:AppDrawerNavigator},
},
{
initialRouteName: 'Login',
});
export default class MMH_App_Final extends Component{
render() {
const App = createAppContainer(AppSwitchNavigator);
return(
<App />
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
crossLogo: {
width:30,
height:30,
marginRight:10
}
});
But on another page I have
<TouchableOpacity
style={styles.recordButton}
onPress={() => navigate('EnterWeight')}
>
I want to be able to navigate to EnterWeight without registering it in the navbar. How can I do that?
a simple way to do this is to manage order on your TabNavigator:
const DashboardTabNavigator = createMaterialBottomTabNavigator({
Dashboard: {screen:Dashboard},
Trends: {screen:Trends},
//Profile: {screen:Profile},
EnterWeight: {screen:EnterWeight}
},
{
barStyle: { backgroundColor: '#000'},
},
{
navigationOptions:({navigation})=>{
const {routeName} = navigation.state.routes[navigation.state.index]
return {
headerTitle: routeName,
order: [
'Dashboard',
'Trends',
//'Profile',
],
}
}
}
);
But you have other options like :
Create a custom TabNavigator
Reorder your stack implementation to
isolate this new screen

How to export Appcontainer and Import in another Screenpage?

my main app is about bottom tab navigator.on that i have using 5 screens.
one screen from the 5 screen i need to use createstacknavigator and i need to export that appcontainer class and import in main screen its not exporting that class saying undefined is not a function(evaluating,(0,b.createAppcontainer)(D)')enter code here
./createStacknavigator
import React from 'react';
import { View, Text, Button } from 'react-native';
import { createAppContainer, createStackNavigator, StackActions, NavigationActions } from 'react-navigation'; // Version can be specified in package.json
class HomeScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
title="Go to Details"
onPress={() => {
this.props.navigation.dispatch(StackActions.reset({
index: 0,
actions: [
NavigationActions.navigate({ routeName: 'Details' })
],
}))
}}
/>
</View>
);
}
}
class DetailsScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
</View>
);
}
}
const AppNavigator = createStackNavigator({
Home: {
screen: HomeScreen,
},
Details: {
screen: DetailsScreen,
},
}, {
initialRouteName: 'Home',
});
export default createAppContainer(AppNavigator);
In MainScreen Page
./Mainscreen
...
import AppNavigator from './components/createStacknavigator.js';
My error is:
undefined is not a function(evaluating,(0,b.createAppcontainer)(D)')
You can use createAppContainer like below:
import React from 'react';
import {
createBottomTabNavigator,
createStackNavigator,
createSwitchNavigator,
createAppContainer
} from 'react-navigation';
const onTabTap = (n) => {
ReactNativeHapticFeedback.trigger('selection', false);
n.defaultHandler();
};
const TabNavigator = createBottomTabNavigator({
Home: {
screen: Jobs,
navigationOptions: () => ({
tabBarOnPress: onTabTap
})
},
Calendar: {
screen: Calendar,
navigationOptions: () => ({
tabBarOnPress: onTabTap
})
}
},
{
defaultNavigationOptions: ({ navigation }) => ({
tabBarIcon: ({ focused }) => {
const { routeName } = navigation.state;
let IconComponent = CONST.IC_HOME;
if (routeName === CONST.MENU_HOME) {
IconComponent = focused ? CONST.IC_HOME_SELECTED : CONST.IC_HOME;
} else if (routeName === CONST.MENU_CALENDAR) {
IconComponent = focused ? CONST.IC_CALENDAR_SELECTED : CONST.IC_CALENDAR;
}
return IconComponent;
}
}),
tabBarOptions: {
activeTintColor: 'red',
inactiveTintColor: 'black',
},
});
const DashBoardStack = createStackNavigator({
DashBoard: TabNavigator
}, {
mode: 'modal',
headerMode: 'none',
});
const App = createSwitchNavigator({
App: DashBoardStack
},
{
initialRouteName: 'App'
});
const AppContainer = createAppContainer(App);
class App extends React.Component {
render() {
return (
<React.Fragment>
<AppContainer />
</React.Fragment>
);
}
}
export default App;

Question on Auth Flow and React Navigation (React Native)

I’m trying to setup the Auth flow with my app, but I can’t seem to figure it out exactly. Here’s how I’d like the flow to be, but please let me know if you have any repositories that might help for this:
Tabs:
Home
Search
Screens:
AuthLoading
Login
Home
Search
Search
Auth Flow:
App launches, checks for userToken
If userToken, redirect to Home
Screen (With Bottom Tab Bar)
If no userToken, redirect to Login Screen (Login Screen has Facebook OAuth from Expo)
User logins with Facebook and checks firebase If success, redirect to Home Screen (With Bottom Tab Bar) 4. If fail, redirect to Login Screen
router.js
import React from 'react';
import { Platform, StatusBar } from 'react-native';
import { createStackNavigator, createBottomTabNavigator, createSwitchNavigator } from 'react-navigation';
import { FontAwesome } from 'react-native-vector-icons';
import AuthLoadingScreen from '../screens/AuthLoadingScreen';
import LoginScreen from '../screens/LoginScreen';
import HomeScreen from '../screens/HomeScreen';
import SearchScreen from '../screens/SearchScreen';
export const UnauthenticatedStack = createStackNavigator({
AuthLoading: {
screen: AuthLoadingScreen,
navigationOptions: {
title: 'AuthLoading',
tabBarVisible: false,
header: null,
headerLeft: null,
headerRight: null,
},
},
Login: {
screen: LoginScreen,
navigationOptions: {
title: 'Login',
tabBarVisible: false,
header: null,
headerLeft: null,
},
},
});
export const AuthenticatedStack = createBottomTabNavigator(
{
Home: {
screen: HomeScreen,
navigationOptions: {
tabBarLabel: 'Home',
tabBarIcon: ({ tintColor }) => (
<FontAwesome name="home" size={24} color={tintColor} />
),
},
},
Search: {
screen: SearchScreen,
navigationOptions: {
tabBarLabel: 'Search',
tabBarIcon: ({ tintColor }) => (
<FontAwesome name="search" size={24} color={tintColor} />
),
},
},
},
);
export default createSwitchNavigator({
Home: AuthenticatedStack,
Login: UnauthenticatedStack,
},
{
initialRouteName: 'Home'
,
});
App.js
// Imports: Dependencies
import React from 'react';
import { View, StyleSheet } from 'react-native';
import firebase from 'firebase';
import { FirebaseAPIKey, authDomain, databaseURL, projectId, messagingSenderId } from './config/config';
import { UnauthenticatedStack, AuthenticatedStack } from './navigation/router';
// Firebase Config
export const firebaseConfig = {
apiKey: FirebaseAPIKey,
authDomain: `${authDomain}`,
databaseURL: `${databaseURL}`,
projectId: `${projectId}`,
// storageBucket: "",
messagingSenderId: `${messagingSenderId}`,
};
console.log(firebaseConfig)
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
// React Native: Application
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<UnauthenticatedStack />
</View>
);
}
};
// Styles
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#CA3433',
},
});
AuthLoading.js
import React from 'react';
import { ActivityIndicator, AsyncStorage, StatusBar, StyleSheet, View, Text } from 'react-native';
export default class AuthLoadingScreen extends React.Component {
constructor(props) {
super(props);
this.checkUserToken();
}
async checkUserToken() {
const userToken = await AsyncStorage.getItem('userToken');
// If User Token
if (userToken) {
AsyncStorage.setItem(userToken);
this.props.navigation.navigate('Home');
}
else {
this.props.navigation.navigate('Login');
}
}
// Render any loading content that you like here
render() {
return (
<View style={styles.container}>
<Text style={styles.text}>Checking Authentication</Text>
<ActivityIndicator />
<StatusBar barStyle="default" />
</View>
);
}
}
// Styles
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#CA3433',
justifyContent: 'center',
alignItems: 'center',
},
text: {
justifyContent: 'center',
color: '#fff',
fontSize: 18,
fontWeight: '500',
},
});
Home.js
// Imports: Dependencies
import React from 'react';
import { View, StyleSheet } from 'react-native';
// Imports: Components
import List from '../components/List';
// React Native Screen: Home
export default () => (
<View style={styles.container}>
<List />
</View>
);
// Styles
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
justifyContent: 'center',
alignItems: 'center',
},
});
Search.js
// Imports: Dependencies
import React, { Component } from 'react';
import { View, Text, StyleSheet } from 'react-native';
// Imports: Components
// React Native Screen: Search
export default class Search extends Component {
// Render
render() {
return (
<View styles={styles.container}>
<Text>Search</Text>
</View>
)
}
}
// Styles
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});
In App.js, do not export firebase config part.
App class should be like that:
export default class App extends React.Component {
componentWillMount() {
const firebaseConfig = {
apiKey: FirebaseAPIKey,
authDomain: `${authDomain}`,
databaseURL: `${databaseURL}`,
projectId: `${projectId}`,
// storageBucket: "",
messagingSenderId: `${messagingSenderId}`,
}
firebase.initializeApp(config);
}
render() {
return (
<View style={styles.container}>
<UnauthenticatedStack />
</View>
)
}
};
Move AuthLoadingScreen to createSwitchNavigator:
const rootNavigator = createSwitchNavigator({
Home: AuthenticatedStack,
Login: UnauthenticatedStack,
AuthLoading: AuthLoadingScreen,
},
{
initialRouteName: 'AuthLoading',
});
export const AppNavigation = createAppContainer(rootNavigator)
App.js
// React Native: Application
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<AppNavigation />
</View>
);
}
};