Cannot get rid of white section at the top of iPhone X emulator - react-native

I just started learning and building an app with react native. Never used xCode before but am using the iPhone X emulator. The login and register screens render fine by themselves. But as soon as I use react navigator, I get this white section at the top and I cannot get rid of it. Please help.
I added SafeAreaView and played around with the various settings but that does not help.
Here is App.js
import React, { Component } from 'react';
import { StyleSheet } from 'react-native';
import { createAppContainer, createStackNavigator, SafeAreaView } from 'react-navigation';
import LoginScreen from './components/pages/LoginScreen.js'
import RegisterScreen from './components/pages/RegisterScreen.js'
export default class App extends Component<Props> {
render() {
return (
<SafeAreaView style={styles.safeArea} forceInset={{ top: 'never' }}>
<AppStackNavigator style={styles.container}/>
</SafeAreaView>
);
}
}
const AppStackNavigator = createAppContainer(createStackNavigator ({
Login: LoginScreen,
Register: RegisterScreen
}))
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#212121',
marginVertical: 40,
},
safeArea: {
flex: 1,
backgroundColor: '#212121',
}
});

Try this:
const AppStackNavigator = createAppContainer(
createStackNavigator({
Login: {
screen: LoginScreen,
navigationOptions: {
header: null,
},
},
Register: RegisterScreen,
}),
);
-
import { getStatusBarHeight } from 'react-native-iphone-x-helper';
class LoginScreen extends React.Component {
render() {
<View style={{ flex: 1, backgroundColor: PRIMARY_COLOR }}>
<SafeAreaView style={{ flex: 1, marginTop: -getStatusBarHeight() }}>
{/* Content */}
</SafeAreaView>
</View>;
}
}

Try react-native-iphone-x-helper

Related

How can I get button-click-navigation and touch-slide-navigation at the same time in React Native?

This is some part of the YouTube video.(using createStackNavigator in React Native).
How can I achieve this kind of effect in React Native( Navigation by button click and touch slide at the same time ) ?
I followed this video but it works such as fadeIn, fadeOut effect in jQuery.
The author of this video installed "yarn add react-navigation#2.0.0-beta.3".
I installed it but it gave me error. So I used this. "yarn add react-navigation".
This is the whole code of App.js.
import React from 'react';
import { Button, View, Text } from 'react-native';
import { createAppContainer } from "react-navigation";
import { createStackNavigator } from 'react-navigation-stack';
class ScreenComponentOne extends React.Component {
render() {
return (
<View
style={{
flex: 1,
alignItems: "center",
justifyContent: "center",
borderWidth: 25,
borderColor: '#f00',
}}
>
<Button
title="Go to the screen two"
onPress={() => {
this.props.navigation.navigate('RouteNameTwo')
}}
/>
</View>
)
}
}
class ScreenComponentTwo extends React.Component {
render() {
return (
<View
style={{
flex: 1,
alignItems: "center",
justifyContent: "center",
borderWidth: 25,
borderColor: '#f0f',
}}
>
<Button
title="Go to the screen one"
onPress={() => {
this.props.navigation.navigate({routeName:'RouteNameOne', transitionStyle: 'inverted'})
}}
/>
</View>
)
}
}
const AppNavigator = createStackNavigator({
RouteNameOne: {
screen:ScreenComponentOne,
navigationOptions:{
header:null,
}
},
RouteNameTwo: {
screen:ScreenComponentTwo,
navigationOptions:{
header:null,
}
},
},
{
initialRouteName:"RouteNameOne"
});
const AppContainer = createAppContainer(AppNavigator);
export default class App extends React.Component {
render() {
return <AppContainer />;
}
}
I'd like to get touch-slide-navigation and button-click-navigation at the same time.
The module that I installed is as follows:
-yarn install
-yarn add react-navigation (for createAppContainer)
-yarn add react-navigation-stack (for createStackNavigator)
How can I achieve this ?

Unable to get tab bar from react-navigation working

I want to show tab bar in my react-native app using react-navigation library. I've tried code from an example provided in the documentation. However, when I run the code on iOS simulator the screen appears to be blank, tab bar doesn't load.
Here's my code so far,
TabNavigator.js
import React from 'react';
import { Text, View } from 'react-native';
import { createBottomTabNavigator, createAppContainer } from 'react-navigation';
class HomeScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Home!</Text>
</View>
);
}
}
class SettingsScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Settings!</Text>
</View>
);
}
}
const TabNavigator = createBottomTabNavigator(
{
Home: HomeScreen,
Settings: SettingsScreen,
},
{
initialRouteName: "Home"
}
);
export default createAppContainer(TabNavigator);
App.js
import React, { Component } from 'react';
import { View, Text, StyleSheet } from 'react-native';
import { createBottomTabNavigator } from 'react-navigation';
import TabNavigator from './components/TabNavigator';
const App = () => {
return (
<View>
<TabNavigator />
</View>
);
};
export default App;
Looking at the App.js code, you forgot to put flex:1 to the view, it's one of the common mistakes that react-navigation tells about in his documentation.
const App = () => {
return (
<View style={ { flex:1} }>
<TabNavigator />
</View>
);
};
Source: https://reactnavigation.org/docs/en/common-mistakes.html#wrapping-appcontainer-in-a-view-without-flex

Error: undefined is not an object (evaluating 'this.props.navigation.navigate')

I have created a separate route file and when I am trying to navigate between screens with the help of these routes with react-navigation using stackNavigator I am getting this error I have already tried other solutions available on Stack overflow but none of the answers solve my problem here is my code.
This is my Route.js file
import { createAppContainer, createStackNavigator } from 'react-navigation';
import Login from './screens/Login';
import Register from './screens/Register';
import Start from './screens/Start';
const AppNavigator = createStackNavigator({
Home: {
screen: Start,
},
LoginScreen: {
screen: Login,
},
RegisterScreen: {
screen: Register,
},
}, {
initialRouteName: 'Home',
navigationOptions: {
header: null
}
});
export default createAppContainer(AppNavigator);
And this is my component where I am trying to navigate
import React,{ Component } from 'react';
import { StyleSheet, View, KeyboardAvoidingView,Text } from 'react-native';
import NewButtons from './NewButtons';
import Logo from './Logo';
export default class Start extends Component{
constructor () {
super();
}
loginPress = () => {
this.props.navigation.navigate('LoginScreen');
}
registerPress = () => {
this.props.navigation.navigate('RegisterScreen');
}
render(){
return(
<KeyboardAvoidingView behavior='padding' style={styles.wrapper}>
<View style={styles.logoContainer}>
<Logo/>
<Text style={styles.mainHead}>Welcome to Food Zone</Text>
<Text style={{margin: 10, textAlign:'center'}}>Check out our menus, order food and make reservations{"\n"}{"\n"}</Text>
<NewButtons text="Login"
onPress={this.loginPress}/>
<NewButtons text="Register"
onPress={this.registerPress}/>
</View>
</KeyboardAvoidingView>
);
}
}
const styles = StyleSheet.create({
logoContainer: {
alignItems: 'center',
},
mainHead: {
fontFamily: 'PatuaOne',
color: '#ff9900',
fontSize: 28,
marginTop: -50,
},
})
And this in my button component
import React,{ Component } from 'react';
import { StyleSheet, View,Dimensions } from 'react-native';
import { Button, Text } from 'native-base';
const {width:WIDTH} = Dimensions.get('window')
export default class NewButtons extends Component{
constructor(props){
super(props);
}
handlePress = () => {
this.props.onPress();
}
render(){
return(
<View style={styles.ButtonContainer}>
<Button light style={styles.mainButtons} onPress={this.handlePress()}>
<Text style={styles.textProps}>{this.props.text}</Text>
</Button>
</View>
);
}
}
const styles = StyleSheet.create({
textProps:{
fontFamily: 'PatuaOne',
color: '#ffffff',
},
mainButtons: {
backgroundColor: "#ff9900",
width: 250,
margin: 5,
color: '#ffffff',
justifyContent: 'center',
width: WIDTH -75,
borderRadius: 10,
},
ButtonContainer: {
alignItems: 'center',
}
})
I managed to remove this error by importing routes in App.js file and by calling it in index.js here is my code of App.js file.
import React, {Component} from "react";
import Routes from "./Routes";
const App = () => <Routes/>
export default App;

ImageBackground component as a container of react-navigation router hiding all child components

I want to set a background image to all of the screens in my react native application,
I am using ImageBackground component on the top level of my components tree like that:
export default class App extends React.Component {
render(){
return(
<View style={{ flex: 1 }}>
<ImageBackground source={require('../assets/app-bg.png')} style={{width: '100%', height: '100%', flex: 1, zIndex: 0, resizeMode: 'cover' }}>
<Router />
</ImageBackground>
</View>)
}
}
and I have the child component which is the router from react-navigation
like that:
class LandingPage extends React.Component {
render(){
return(
<View style={{flex: 1, zIndex: 999}}>
<Text>here is landing page></Text>
</View>
)
}
}
const RouterNavigator = createAppContainer(createStackNavigator({
Landing: {
screen: Landing,
navigationOptions:{
header: null
}
}
}
export default class Router extends React.Component {
render() {
return <RouterNavigator style={{flex: 1}}/>
}
}
the problem is that the background image is being rendered but the child component LandingPage is being hidden even though it is being rendered too!
Just have a look at this example.Does this help you acheive what you were
trying to.
import * as React from 'react';
import { Text, View, StyleSheet, ImageBackground } from 'react-native';
import { Constants } from 'expo';
import AssetExample from './components/AssetExample';
import { createAppContainer, createStackNavigator } from 'react-navigation';
import { Card } from 'react-native-paper';
class LandingPage extends React.Component {
render() {
return (
<View>
<Text>here is landing page</Text>
</View>
);
}
}
const RouterNavigator = createAppContainer(
createStackNavigator(
{
LandingPage: {
screen: LandingPage,
navigationOptions: {
header: null,
},
},
},
{
mode: 'card',
transparentCard: true,
cardStyle: { backgroundColor: 'transparent' },
transitionConfig: () => ({
containerStyle: {
backgroundColor: 'transparent',
},
}),
initialRouteName: 'LandingPage',
}
)
);
export default class App extends React.Component {
render() {
return (
<ImageBackground
source={require('./bgimage.jpeg')}
style={{
flex: 1,
}}>
<RouterNavigator />
</ImageBackground>
);
}
}

React Navigation Issue with NavigationActions

I have an application in which I am trying to apply navigation in some case. For example, A, B, C, D are my tabs and in the tab A, I have 3 pages as 1, 2, 3. Now in a situation where I am on tab A and on page 3, there is one button present in which I have to navigate back to page 1 on the same tab itself (A).
But somehow, my navigation is not working in this case. Below are my codes:
SkippedTask.js
import React from "react";
import {
View,
StyleSheet,
Text,
AsyncStorage,
Dimensions,
Image,
ScrollView,
Alert
} from "react-native";
import ButtonC from "../../components/ButtonC";
import { NavigationActions } from "react-navigation";
import Server from "../../provider/Server";
var { width, height } = Dimensions.get("window");
export default class SkippedTask extends React.Component {
constructor(props) {
super(props);
this.state = {
loadingState: false,
api: new Server(),
mobile: "",
arrayLenght: "",
tasklist: [],
taskTitle: []
};
}
renderSkippedQues() {
return (
<View style={{ marginTop: 0 }}>
{this.state.taskTitle.map((item, key) => (
<View key={key} style={{ marginTop: 0 }}>
<Text style={{}}>
<Text style={{ fontSize: 18, color: "#000" }}>{"\u2022"} </Text>{" "}
{item}
</Text>
</View>
))}
</View>
);
}
handleNav = () => {
this.props.navigation.dispatch(
NavigationActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: "DashboardTab" })]
})
);
};
render() {
if (this.state.arrayLenght <= 0) {
return <View />;
} else {
return (
<ScrollView>
<View
style={{
justifyContent: "center",
alignItems: "center",
marginTop: height / 40
}}
>
<Text style={{ fontSize: 16, color: "black", marginTop: 30 }}>
List of skipped task are :
</Text>
{this.renderSkippedQues()}
<View style={{ marginTop: 40 }} />
<ButtonC
title=" Click here to complete remaining task "
onPress={this.handleNav}
/>
</View>
</ScrollView>
);
}
}
}
Below is my App.js file in which I am using StackNavigator of react-navigation.
/**
* Sample React Native App
* https://github.com/facebook/react-native
* #flow
*/
import { AppRegistry } from 'react-native';
import { StackNavigator } from 'react-navigation';
import SessionCheck from './src/screens/SessionCheck';
import LoginScreen from './src/screens/LoginScreen';
import RegisterScreen from './src/screens/RegisterScreen';
import ProfileScreen from './src/screens/ProfileScreen';
import WalletsScreen from './src/screens/WalletsScreen';
import DashboardAccept from './src/screens/DashboardAccept';
import TermsConditionsScreen from './src/screens/TermsConditionsScreen';
import DashboardTabScreen from './src/screens/DashboardTabScreen';
import Person from './src/components/profile/Person';
import Payment from './src/components/profile/Payment';
import List from './src/components/task/List';
const App = StackNavigator({
// DashboardTab: { screen:DashboardTabScreen },
Home: { screen: SessionCheck },
Login: { screen: LoginScreen },
Register: { screen: RegisterScreen },
Profile: { screen:ProfileScreen },
Wallets: { screen:WalletsScreen },
DashboardAccept: { screen:DashboardAccept },
TermsConditions: { screen:TermsConditionsScreen },
DashboardTab: { screen:DashboardTabScreen },
Person: {screen: Person},
Payment : {screen: Payment},
AllTask:{screen: List}
});
export default App;
Now my case lies that I need to navigate from the SkippedTask page to AllTask screen as mentioned in the App.js file. But somehow I am not able to resolve this issue.
Any leads are appreciated. Thanks.