React native - Stack Screen flikering on authentication flow - react-native

React native Login Stack Blinks while opening the app, before showing the home page. Using react native navigation V5.
Adding the Token from server to async Storage, then on opening the app, i check for the token in Async storage, if it's not null, i'll navigate to my AppScreens else stay in AuthScreens
AppNavigator
import React, {useState, useEffect} from 'react';
import {connect} from 'react-redux';
import {createStackNavigator} from '#react-navigation/stack';
import AsyncStorage from '#react-native-community/async-storage';
import AddUserData from '../lib/UserDetailsHelper';
//Import screens
import Login from '../Login';
import AddReferalCode from '../AddReferalCode';
import AddUserDetails from '../AddUserDetails';
import DrawerNavigator from './DrawerNavigator';
import BuyCoins from '../BuyCoins';
import PriceList from '../PriceList';
//Quick Play
import QuickPlayGameList from '../QuickPlay/GameList';
import QuickplayRoom from '../QuickPlay/RoomCreationScreen';
import QuickPlayWaitingRoom from '../QuickPlay/waitingRoom';
import QuickPlayQuestions from '../QuickPlay/Questions';
import QuickPlayResult from '../QuickPlay/Result';
import RNBootSplash from 'react-native-bootsplash';
const Stack = createStackNavigator();
const AuthStack = createStackNavigator();
const AppStack = createStackNavigator();
const AuthScreens = (props) => (
<AuthStack.Navigator
screenOptions={{
headerShown: false,
headerTransparent: true,
headerTitle: false,
}}>
{/* Common */}
<Stack.Screen {...props} name="Login" component={Login} />
<Stack.Screen name="AddReferalCode" component={AddReferalCode} />
<Stack.Screen name="Add User Details" component={AddUserDetails} />
</AuthStack.Navigator>
);
const AppScreens = () => (
<AppStack.Navigator
screenOptions={{
headerShown: false,
headerTransparent: true,
headerTitle: false,
}}>
<Stack.Screen name="Drawer" component={DrawerNavigator} />
<Stack.Screen name="BuyCoins" component={BuyCoins} />
<Stack.Screen name="PriceList" component={PriceList} />
{/* <Stack.Screen name="Home" component={Home} /> */}
{/* QuickPlay */}
<Stack.Screen name="QuickPlayGameList" component={QuickPlayGameList} />
<Stack.Screen name="QuickplayRoom" component={QuickplayRoom} />
<Stack.Screen
name="QuickPlayWaitingRoom"
component={QuickPlayWaitingRoom}
/>
<Stack.Screen name="QPQuestions" component={QuickPlayQuestions} />
<Stack.Screen name="QPResult" component={QuickPlayResult} />
</AppStack.Navigator>
);
Routes = (props) => {
const [token, setToken] = useState(null);
console.log('Props acc token:' + props.accessToken);
useEffect(() => {
getToken()
.then((token) => {
setToken(token);
})
.finally(async () => {
await RNBootSplash.hide({fade: true});
});
}, []);
const getToken = async () => {
try {
const token = await AsyncStorage.getItem('AccessToken');
const rating = await AsyncStorage.getItem('UserRating');
console.log('first token:' + token);
setToken(token);
AddUserData.addAccessToken(token);
AddUserData.addUserRating(rating);
return token;
} catch (e) {
console.log(e);
}
};
return token ? <AppScreens /> : <AuthScreens />;
};
const mapStateToProps = (state) => {
return {
accessToken: state.userDetails.accessToken,
};
};
export default connect(mapStateToProps, null)(Routes);
The AuthScreen Stack blinking when opening the app.
ScreenRecoding of the issue
https://drive.google.com/file/d/12p2NtxUbfS-eFt5iGM0PAvhuJZJhUyJQ/view?usp=sharing

Please try placing your home screen BEFORE the login screen and see the effet
<AuthStack.Navigator
screenOptions={{
headerShown: false,
headerTransparent: true,
headerTitle: false,
}}>
// >>> PUT HOME SCREEN HERE
{/* Common */}
<Stack.Screen {...props} name="Login" component={Login} />
<Stack.Screen name="AddReferalCode" component={AddReferalCode} />
<Stack.Screen name="Add User Details" component={AddUserDetails} />
</AuthStack.Navigator>
Additional information: (thanks for Thorin's discovery)
Try to add a Splash screen to the project, and add a small time out of say 10ms, it may be able to fix the problem.

Related

Mixing both Stack navigation and tab navigation in react native

I'm trying to mix stack navigation and tab navigation i have 14 Screens:
Search,
Home,
CartTab,
SideBar,
Details,
PriceComparison,
ShopByCategory,
ShopByStore,
CategoryPage,
CategoryFilter,
Checkout,
Login,
Register,
ForgotPassword
I want to have Search, Home, CartTab, Sidebar as bottom navigation and the other pages as stack navigation, with SearchBy pages, Details, Price Comparison not displaying the bottom tab. I'm new to react native and i have been trying diffrent solutions however 1) i'm not able to remove the bottomtab from the wanted screens and 2) using navigation.navigate is returning me errores example using navigation.navigate('Category'); i get an error The action 'NAVIGATE' with payload {"name":"Category"} was not handled by any navigator. Do you have a screen named 'Category'?
This is my code :
StackNavigator.js:
import React from 'react'
import { createStackNavigator } from '#react-navigation/stack';
import { NavigationContainer } from '#react-navigation/native';
import {
CartTab,
Details,
Home,
Notification,
PriceComparison,
Search,
SplashScreen,
ShopByCategory,
ShopByStore,
CategoryPage,
Checkout,
CategoryFilter,
Login,
Register,
ForgotPassword
} from '../screens';
import { SideBar } from './../components';
export const MainStackNavigator = () => {
const Stack = createStackNavigator();
return (
<Stack.Navigator screenOptions={{headerShown: false}} initialRouteName={'Home'}>
{/*<Stack.Screen name="SplashScreen" component={SplashScreen} /> */}
<Stack.Screen name="Login" component={Login} options={{ tabBarVisible: false }} />
<Stack.Screen name="Register" component={Register} options={{ tabBarVisible: false }} />
<Stack.Screen name="Recovery" component={ForgotPassword} options={{ tabBarVisible: false }} />
<Stack.Screen name="Home" component={Home} options={{ tabBarVisible: false }} />
<Stack.Screen name="Notification" component={Notification} />
<Stack.Screen name="Comparison" component={PriceComparison} options={{ tabBarVisible: false }} />
<Stack.Screen name="ShopByCategory" component={ShopByCategory}/>
<Stack.Screen name="ShopByStore" component={ShopByStore}/>
{/*<Stack.Screen name="Category" component={CategoryPage}/>
<Stack.Screen name="CategoryFilter" component={CategoryFilter}/>*/}
</Stack.Navigator>
);
}
export const CartStackNavigator = () => {
const Stack = createStackNavigator();
return (
<Stack.Navigator screenOptions={{headerShown: false}}>
<Stack.Screen name="Cart" component={CartTab} />
<Stack.Screen name="Checkout" component={Checkout}/>
</Stack.Navigator>
);
}
export const SearchStackNavigator = () => {
const Stack = createStackNavigator();
return (
<Stack.Navigator screenOptions={{headerShown: false}}>
<Stack.Screen name="Search" component={Search} />
<Stack.Screen name="Details" component={Details}/>
</Stack.Navigator>
);
}
export const SidebarStackNavigator = () => {
const Stack = createStackNavigator();
return (
<Stack.Navigator screenOptions={{headerShown: false}}>
<Stack.Screen name="Sidebar" component={SideBar} />
<Stack.Screen name="ShopByCategory" component={ShopByCategory}/>
<Stack.Screen name="ShopByStore" component={ShopByStore}/>
<Stack.Screen name="Category" component={CategoryPage}/>
</Stack.Navigator>
);
}
TabNavigation.js:
import 'react-native-gesture-handler';
import React from 'react';
import {createBottomTabNavigator} from '#react-navigation/bottom-tabs';
import Ionicons from 'react-native-vector-icons/Ionicons';
import {COLORS} from '../constants';
import { CartStackNavigator, MainStackNavigator, SearchStackNavigator, SidebarStackNavigator }
from '../navigation/StackNavigator';
import { useStateContext } from '../context/StateContext';
const Tab = createBottomTabNavigator();
export const TabNavigation = ({navigation}) => {
return (
<Tab.Navigator
screenOptions={({ route }) => ({
headerShown: false,
tabBarIcon: ({ focused, color, size }) => {
let iconName;
if (route.name === 'Home') {
iconName = focused
? 'home'
: 'home-outline';
} else if (route.name === 'Cart') {
iconName = focused ? 'cart' : 'cart-outline';
} else if (route.name === 'Search') {
iconName = focused ? 'search' : 'search-outline';
} else if (route.name === 'SideBar') {
iconName = focused ? 'grid' : 'grid-outline';
}
// You can return any component that you like here!
return <Ionicons name={iconName} size={size} color={color} />;
},
tabBarActiveTintColor: COLORS.primary,
tabBarInactiveTintColor: 'gray',
})}
>
<Tab.Screen name="Home" component={MainStackNavigator} />
<Tab.Screen name="Search" component={SearchStackNavigator} />
<Tab.Screen name="Cart" component={CartStackNavigator} options={{ tabBarBadge: 3, tabBarBadgeStyle: { backgroundColor: COLORS.primary, marginTop: -5 } }}/>
<Tab.Screen name="SideBar" component={SidebarStackNavigator} />
</Tab.Navigator>
);
};
App.js:
import React from "react";
import { createStackNavigator } from '#react-navigation/stack';
import { NavigationContainer } from '#react-navigation/native';
import CustomDrawer from './navigation/CustomDrawer'
import {StateContext} from './context/StateContext'
import { Home, CartTab, Search, SideBar, SplashScreen, Notification, Login, Register,
ForgotPassword } from "./screens";
import { Header, TabNavigation } from "./components";
import { StripeProvider } from '#stripe/stripe-react-native';
const Stack = createStackNavigator();
const App = () => {
const [isLoading, setIsLoading] = React.useState(true);
React.useEffect(() => {
setTimeout(() => {
setIsLoading(false);
}, 3000);
}, []);
if (isLoading) {
return <SplashScreen />;
}
return (
<StateContext>
<NavigationContainer>
<TabNavigation/>
</NavigationContainer>
</StateContext>
);
}
export default App
Make two different Stacks file one is StackNavigation and other is BottomNavigation . Render your Stack Navigation and with calling the component of TabNavigation
I used the Dummy names of screens . Replace it according to your screen names
Stack navigation
import { createStackNavigator } from '#react-navigation/stack';
import { NavigationContainer } from '#react-navigation/native';
import {
CartTab,
Details,
Home,
} from '../screens';
import {TabNavigation } from '../navigation/StackNavigator';
export const StackNavigator = () => {
const Stack = createStackNavigator();
return (
<Stack.Navigator screenOptions={{headerShown: false}}
initialRouteName={'Home'}>
<Stack.Screen name="Home" component={TabNavigation}/>
<Stack.Screen name="ShopByCategory" component={ShopByCategory}/>
<Stack.Screen name="ShopByStore" component={ShopByStore}/>
<Stack.Screen name="CategoryFilter" component={CategoryFilter}/>
</Stack.Navigator>
);
}
Bottom Tab Navigation
import {createBottomTabNavigator} from '#react-navigation/bottom-tabs';
const Tab = createBottomTabNavigator();
export const TabNavigation = ({navigation}) => {
return (
<Tab.Navigator
screenOptions={({ route }) => ({
headerShown: false}
})}>
<Tab.Screen name="Example" component={Example} />
<Tab.Screen name="Search" component={Search} />
<Tab.Screen name="SideBar" component={SideBar} />
</Tab.Navigator>
);
};

Navigate between two stack Navigators after login in react native

This is my first stack
<NavigationContainer>
<Stack.Navigator
initialRouteName="Home"
screenOptions={{
headerShown: false,
}}
>
<Stack.Screen name="Home" component={MainScreen} />
<Stack.Screen name="Schools" component={SchoolsScreen} />
<Stack.Screen name="Setting" component={SettingScreen} />
<Stack.Screen name="Profile" component={ProfileScreen} />
<Stack.Screen name="Saved" component={SavedData} />
<Stack.Screen name="Profile2" component={Profile2} />
<Stack.Screen name="SchoolDetails" component={SchoolDetailsScreen} />
<Stack.Screen name="Bottom" component={BottomTab} />
</Stack.Navigator>
</NavigationContainer>
This is my second stack
<NavigationContainer>
<Stack.Navigator
initialRouteName="Login"
screenOptions={{
headerShown: false,
}}
>
<Stack.Screen name="Login" component={AuthLogin} />
<Stack.Screen name="Register" component={AuthRegister} />
<Stack.Screen name="Forget" component={ForgetPassword} />
</Stack.Navigator>
</NavigationContainer>
THis is my app.js file
const [auth, setAuth] = useState(false);
useEffect(() => {
(async () => {
const value = await AsyncStorage.getItem("isLoggedin");
console.log(value);
setAuth(value);
})();
}, []);
return (
<SafeAreaView style={{ flex: 1 }}>
<NativeBaseProvider config={config}>
{auth == "true" ? <InsideStack /> : <OutsideStack />}
</NativeBaseProvider>
</SafeAreaView>
);
}
I want to navigate to home screen from login screen , after function call
// navigation.push("Home");
const storeData = async (value) => {
try {
await AsyncStorage.setItem("isLoggedin", JSON.stringify(true));
} catch (e) {
// saving error
}
};
storeData();
navigation.push("Home");
}}
But i got error:
Do you have a screen named 'Home'?
this is the simple code for this. you have to use redux or context api for gloal state management. here is a min example ;
import {NavigationContainer} from '#react-navigation/native';
import React from 'react';
import {useDispatch, useSelector} from 'react-redux';
import {GetUserContacts} from '../logic/getcontact';
import {setContacts} from '../store/actions/ContactActions';
import AuthNavigation from './AuthNavigation';
import RootNavigation from './RootNavigation';
const AppContainer = () => {
const {is_logged_in} = useSelector(state => state.persistedReducer);
const dispatch = useDispatch();
if (is_logged_in) {
GetUserContacts('PK').then(data => {
dispatch(setContacts(data));
});
}
return (
<NavigationContainer>
{is_logged_in ? <RootNavigation /> : <AuthNavigation />}
</NavigationContainer>
);
};
export default AppContainer;
after user logged in you have to make that is_logged_in true. so the navigator changes itself. you will have to persist this so when application restart the user dont need to login again

Combine Drawer.Navigator and Stack.Navigator without creating separate navigation bars

I would like to have stack and drawer navigators use the same navigation bar. However, both are creating their own navigation bars refer to the below image making the app look messy. How can I achieve that?
Below is my code:
import * as React from "react";
import { createStackNavigator } from "#react-navigation/stack";
import { createDrawerNavigator } from "#react-navigation/drawer";
import Home from "../screens/Home";
import Contact from "../screens/Contact";
import About from "../screens/About";
const Stack = createStackNavigator();
const Drawer = createDrawerNavigator();
const HomeNavigation = (props) => {
return (
<Stack.Navigator initialRouteName='Home'
screenOptions={{
headerTitleAlign:'center',
//headerShown: false
}}
>
<Stack.Screen name='Home' component={Home} />
<Stack.Screen name='Contact' component={Contact} />
<Stack.Screen name='About' component={About} />
</Stack.Navigator>
);
};
const MenuNavigation = (props) => {
return (
<Stack.Navigator initialRouteName='Home'
screenOptions={{
headerTitleAlign:'center',
//headerShown: false
}}>
<Stack.Screen name='Menu' component={About} />
</Stack.Navigator>
);
};
const DrawerNavigation = () => {
return (
<Drawer.Navigator>
<Drawer.Screen name='HomeNavigation' component={HomeNavigation} />
<Drawer.Screen name='MenuNavigation' component={MenuNavigation} />
</Drawer.Navigator>
);
};
export default DrawerNavigation;

How to test navigation button on press

Let's say I have one button and when user input their name and that name is valid then button will enabled and on press it will navigate to other screen.
But when I try to test on press button, it will return error.
TypeError: Cannot read property 'navigate' of undefined
This is my component
import React, { useState } from 'react'
import { ScrollView, StatusBar, Text, View } from 'react-native'
import HeaderAuth from '../../../__global__/headerAuth/headerAuth'
import color from '../../../__global__/styles/themes/colorThemes'
import ButtonFull from '../../../__global__/button/buttonFull'
import Field from '../../../__global__/fieldAuthScreen/Field'
import styles from './styles/StyleFullName'
import regex from '../../../constant/regex'
const FullNameScreen = ({ navigation }) => {
const [fullName, setFullName] = useState('')
const regexName = regex.name
const submitFullName = () => {
navigation.navigate('LinkEmail')
}
return (
<ScrollView>
<StatusBar
translucent
backgroundColor="transparent"
barStyle="light-content"
/>
<HeaderAuth />
<Field
testID={'inputFullName'}
icon={'account'}
value={fullName}
placeholder={'Type your full name'}
onChangeText={(value) => {
handleChange('fullName')
setFullName(value)
setFieldTouched('fullName', true)
setFieldValue('fullName', value)
}}
onBlur={() => setFieldTouched('fullName', true)}
keyboardType={'default'}
/>
<ButtonFull
testID={'submitFullName'}
isDisabled={!isValid}
buttonColor={isValid ? color.thema : color.disabledButton}
onPress={() => submitFullName()}
title={'Next'}
/>
</ScrollView>
)
}
export default FullNameScreen
This is my test file
import React from 'react'
import { configure, shallow, mount } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
import FullNameScreen from '../index'
import renderer from 'react-test-renderer'
import { fireEvent, render, waitFor } from 'react-native-testing-library'
import '#testing-library/jest-native/extend-expect'
jest.mock('#react-navigation/native', () => ({
useNavigation: component => component,
}))
describe('Login', () => {
configure({ adapter: new Adapter() })
const wrapper = shallow(<FullNameScreen />)
const instaceOf = wrapper.instance()
const rendered = renderer.create(<FullNameScreen />)
it('renders correctly', () => {
expect(rendered.toJSON()).toBeTruthy()
})
it('should call submit fullname one time', async () => {
const onPressMock = jest.fn()
const phoneNumber = 'Ganda Rain Panjaitan'
const { getByTestId } = render(<FullNameScreen />)
const input = getByTestId('inputFullName')
fireEvent.changeText(input, phoneNumber)
expect(getByTestId('inputFullName').props.value).toEqual(phoneNumber)
await waitFor(() => {
expect(getByTestId('submitFullName')).toBeEnabled()
})
fireEvent.press(getByTestId('submitFullName'))
expect(onPressMock.mock.calls.length)
})
})
This is my navigator
import * as React from 'react'
import { NavigationContainer } from '#react-navigation/native'
import { createStackNavigator } from '#react-navigation/stack'
import HomeScreen from '../views/homeScreen'
import SplashScreen from '../views/splashScreen'
import WelcomeScreen from '../views/welcomeScreen'
import ProfileScreen from '../views/profileScreen'
import ChangeNumberScreen from '../views/changeNumberScreen'
import LoginScreen from '../views/authScreen/loginScreen'
import PinScreen from '../views/authScreen/pinScreen/index'
import SetPinScren from '../views/authScreen/setPinScreen'
import OtpVerificationScreen from '../views/authScreen/otpVerificationScreen'
import PhoneNumberScreen from '../views/authScreen/phoneNumberScreen'
import FullNameScreen from '../views/authScreen/fullNameScreen'
import LinkEmailScreen from '../views/authScreen/linkEmailScreen'
const Stack = createStackNavigator()
import ChangeNameScreen from '../views/changeNameScreen'
import MainTab from '../views/mainTab'
function Routes() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Splash" component={SplashScreen} options={{ headerShown: false }} />
<Stack.Screen name="Welcome" component={WelcomeScreen} options={{ headerShown: false }} />
<Stack.Screen name="Home" component={HomeScreen} options={{ headerShown: false }} />
<Stack.Screen name="Profile" component={ProfileScreen} options={{ headerShown: false }} />
<Stack.Screen name="ChangeNumber" component={ChangeNumberScreen} options={{ headerShown: false }} />
<Stack.Screen name="Login" component={LoginScreen} options={{ headerShown: false }} />
<Stack.Screen name="Pin" component={PinScreen} options={{ headerShown: false }} />
<Stack.Screen name="SetPin" component={SetPinScren} options={{ headerShown: false }} />
<Stack.Screen name="OtpVerification" component={OtpVerificationScreen} options={{ headerShown: false }} />
<Stack.Screen name="PhoneNumber" component={PhoneNumberScreen} options={{ headerShown: false }} />
<Stack.Screen name="ChangeName" component={ChangeNameScreen} options={{ headerShown: false }} />
<Stack.Screen name="MainTab" component={MainTab} options={{ headerShown: false }} />
<Stack.Screen name="FullName" component={FullNameScreen} options={{ headerShown: false }} />
<Stack.Screen name="LinkEmail" component={LinkEmailScreen} options={{ headerShown: false }} />
</Stack.Navigator>
</NavigationContainer>
)
}
export default Routes
Set it like this:
const FullNameScreen = ({ navigation: { navigate } }) => {
yourFunction() {
navigate('LinkEmail')
}
}
Or alternately you can pass full props like this:
const FullNameScreen = (props) => {
yourFunction() {
props.navigation.navigate('LinkEmail')
}
}

Open the drawer when I click the icon react navigation version 5

I want to open the drawer when I click the icon in the headerLeft part, I also have try to this.props.navigation.dispatch but is gives an error also navigation.dispatch gives error
The code below does not gives errors but is does not open the drawer
import { DrawerActions } from '#react-navigation/native';
import { NavigationContainer } from '#react-navigation/native';
import { createDrawerNavigator } from '#react-navigation/drawer';
import { createStackNavigator } from '#react-navigation/stack';
const Drawer = createDrawerNavigator();
const Stack = createStackNavigator();
export default class App extends Component {
createHomeStack = () =>
<Stack.Navigator>
<Stack.Screen
initialRouteName="login"
headerMode="screen"
name="main"
children={ this.createBottomTabs}
options={{
title: "Fitbit",
headerLeft: () => (
<Icon
name="menu"
size={25}
color="#D4AF37"
onPress={() => {DrawerActions.openDrawer() }}
/>
)} } />
</Stack.Navigator>
createDrawer = ({navigation}) =>
<Drawer.Navigator initialRouteName="Main" >
<Drawer.Screen name="Main" component={Main} />
<Drawer.Screen name="Contacts" component={Food} />>
</Drawer.Navigator>
render() {
return (
<NavigationContainer>
{this.createHomeStack()}
</NavigationContainer>
);
}
}
In order to achieve that you need to wrap the stack into the drawer as the documentation says.
Documentation here
I would probably use something like this:
EDIT:
Added full code
import React,{Component} from 'react'
import { NavigationContainer } from '#react-navigation/native'
import { createDrawerNavigator } from '#react-navigation/drawer'
import { createStackNavigator } from '#react-navigation/stack'
import { View } from 'react-native'
import Icon from 'react-native-vector-icons/dist/Feather'
const Drawer = createDrawerNavigator()
const Stack = createStackNavigator()
const Main = () => <View></View>
const Food = () => <View></View>
const Home = ({ navigation }) => (
<Stack.Navigator>
<Stack.Screen name="Main" component={Main} options={{
headerLeft: () => <Icon
name="menu"
size={25}
color="#D4AF37"
onPress={() => navigation.openDrawer()}
/>
}} />
</Stack.Navigator>
)
const DrawerNavigator = () => (
<Drawer.Navigator initialRouteName="Home">
<Drawer.Screen name="Home" component={Home} />
<Drawer.Screen name="Contacts" component={Food} />
</Drawer.Navigator>
)
export default props => (
<NavigationContainer>
<DrawerNavigator />
</NavigationContainer>
)
You need To import DrawerActions
import {DrawerActions } from '#react-navigation/native';
And You Can Open Using
navigation.dispatch(DrawerActions.openDrawer());