React-native app header overlaps with the phone header - react-native

I am using react-navigation to add custom header to my app. I am facing the same issue that is shown in the left image where the phone header overlaps with app's header. I tried to add SafeAreaView around my screen. But, it didn't make solve the problem.

it has two solutions. you can use
import React from 'react'
import {View,SafeAreaView, Text} from 'react-native'
const ConvertingFileScreen = () => {
return(
<SafeAreaView>
<Text>ConvertingFileScreen</Text>
</SafeAreaView>
)
}
export default ConvertingFileScreen;
or simply with giving a marginTop of StatusBar.height like this.
import React from 'react'
import {View,SafeAreaView,StatusBar, Text,} from 'react-native'
const ConvertingFileScreen = () => {
return(
<View style={{marginTop:StatusBar.currentHeight}}>
<Text>ConvertingFileScreen</Text>
</View>
)
}
export default ConvertingFileScreen;

You can use SafeAreaView or you can add some top spaces using useSafeAreaInsets from 'react-native-safe-area-context' It will solve the issue.
import React from 'react';
import { View,Text } from 'react-native';
import {useSafeAreaInsets} from 'react-native-safe-area-context';
const ConvertingFileScreen = () => {
const { top } = useSafeAreaInsets();
return (
<View style={{marginTop: top}}>
<Text>HI React Native</Text>
</View>
)
}
Check for more details

Related

I am using Navigate to go to another page on React Native, I've tried with navigation & React Dom

I am using Navigate to go to another page on React Native, I've tried with navigation & react Dom. I want to go to Home.js. I've tried using NavigationContainer, & reateStackNavigator and also React Dom.
I can't seem to find a solution to either of the two and the YouTube videos aren't really helping.
import {
StyleSheet,
Text,
SafeAreaView,
Image,
View,
TouchableOpacity,
} from 'react-native';
import React,{ useState} from 'react';
import {Card} from 'react-native-shadow-cards';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
import {Route, BrowserRouter as Router, Switch, Link, Navigate, NavigationType} from 'react-router-dom';
export default function App() {
const handlePress = () => navigation.navigate('Home');
const [name,setName] = useState("");
const shadowstyle={
shadowOpacity:1
};
return (
<SafeAreaView style={styles.container}>
<Image
source={require("/Users/travisnunnally/Malik Projects VSCODE/Gtipz/assets/Star.png")} />
<Image
source={require("/Users/travisnunnally/Malik Projects VSCODE/Gtipz/assets/Star.png")} />
<Text style={styles.titleText} onPress={handlePress}>GTipz </Text>
<Image
source={require("/Users/travisnunnally/Malik Projects VSCODE/Gtipz/assets/GtipzLogo.png")} />
<View>
<Image
source={require("/Users/travisnunnally/Malik Projects VSCODE/Gtipz/assets/Star.png")} />
</View>
<TouchableOpacity onPress={handlePress}>
<View style ={[styles.buttonContainer, styles.userImageWrapper]}>
<Text style={styles.buttonText}>Login</Text>
</View>
You need to import the useNavigation hook from react-navigation.
import { useNavigation } from '#react-navigation/native'
const navigation = useNavigation()
I also suggest you replace Text with TouchableOpacity where you call the handlePress function
Please refer to this doc for more information.

how to use a custom functional component in react-native?

how to use a functional custom component in react native??? i actualy build a login functional component like a custom react but not exsist a "div" i made a "View"
why should this not work??
import React from 'react';
import { Text, View } from 'react-native';
const Login = () => {
return (
<View>
<Text>Hi</Text>
</View>
);
}
export default Login;
---------------------------------
import React from 'react';
import {Login} from './components/Login/Login';
import { Text, View } from 'react-native';
const App = () => {
return (
<View>
<Login/>
</View>
)
}
export default App;
You've got your named exports and default exports turned around.
This would work instead:
import React from 'react';
import { Text, View } from 'react-native';
const Login = () => {
return (
<View>
<Text>Hi</Text>
</View>
);
}
export default Login;
---------------------------------
import React from 'react';
import Login from './components/Login/Login';
import { Text, View } from 'react-native';
const App = () => {
return (
<View>
<Login/>
</View>
)
}
export default App;
When you export default Login, you must import it as a default import with import Login from './components/Login/Login'
Here is an Expo Snack with the working code as well

How to implement custom toggleSwitch in React Native?

I am implementing custom toggleSwitch in react native.I am new in react native. I gone through below link but I am not able achieve the implement.
https://www.digitalocean.com/community/tutorials/how-to-build-a-custom-toggle-switch-with-react
I also check https://dev.to/narendersaini32/how-to-create-custom-toggle-button-in-react-387m
Login.js
import React from 'react';
import styles from './style';
import {
SafeAreaView,
View,
Switch
} from "react-native";
class Login extends React.Component {
render() {
return (
<SafeAreaView>
<View style={styles.container}>
</View>
<View>
<ToggleComponent/> //I want to import here toggleSwitch
</View>
</View>
</SafeAreaView>
)
}
}
export default Login;
ToggleButton.js
import React from 'react';
const ToggleComponent = ({}) => (
*****************??**************
);
const styles = StyleSheet.create({
})
export default ToggleComponent;
I want to implement custom ToggleSwitch.
import this component in your file where you want to use it.
import ToggleComponent from "../location_of_ToggleComponent.js"
and then you can use it like any other component.
<ToggleComponent />

React Native (Expo) - useWindowDimensions().width not updated on orientation change

I'm stuck with element sizes based on screen sizes when device orientation changes.
Here's my code
import React, { useEffect, useState } from 'react';
import { View, Text, TouchableOpacity, useWindowDimensions } from 'react-native';
const CleanSearchBar = (props) => {
const windowWidth = useWindowDimensions().width;
return (
<View>
<TouchableOpacity style={{left: (windowWidth - 127)}}>
<Text>text</Text>
</TouchableOpacity>
</View>);
}
It always keeps the initial value.
Any help on this? Thanks

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

////////////this is homescreen, here i try to go to 'Components'. it's not recognize 'props.navigation.navigate
import React from 'react';
import { Text, StyleSheet, View, Button, TouchableOpacity } from 'react-native';
const HomeScreen = props => {
return (
<View>
<Text style={styles.text}>Hi there !</Text>
<Button
onPress={() => props.navigation.navigate('Components')}
title="Go to Componetes demo"
/>
</View>
);
};
const styles = StyleSheet.create({
text: {
fontSize: 30
}
});
export default HomeScreen;
/////////This is the index.js here i am using homescreen.
import React from 'react';
import { AppRegistry,View } from 'react-native';
import HomeScreen from './src/screens/HomeScreen';
import ListScreen from './src/screens/ListScreen';
import ComponentsScreen from './src/screens/ComponentsScreen';
import Header from './src/screens/Header';
const App = () => {
return(
<View style={{ flex: 1 }}>
<Header headerText={'Hello ! '}/>
<HomeScreen />
</View>
);
};
AppRegistry.registerComponent('Tal', () => App);
This is the App.js,and the navigator is here. This is edited.
i Need help .///////////////////////////////////////////////
/////##########//////////
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* #format
* #flow
*/
import React, {Fragment} from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
} from 'react-native';
import {
Header,
LearnMoreLinks,
Colors,
DebugInstructions,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
import { createStackNavigator } from 'react-navigation-stack';
import { createAppContainer } from 'react-navigation';
const navigator = createStackNavigator (
{
Home : HomeScreen,
Components : ComponentsScreen,
List : ListScreen
},
{
initialRouteName : 'Home',
defaultNavigationoptions : {
title : App
}
}
);
export default createAppContainer(navigator);
First, you need to understand how are you gonna have that navigation as prop inside a component.
Each screen component in your app is provided with the navigation prop automatically. It's important to highlight the navigation prop is not passed into all components; only screen components receive this prop automatically! React Navigation doesn't do anything magic here. For example, if you were to define a MyBackButton component and render it as a child of a screen component, you would not be able to access the navigation prop on it. If, however, you wish to access the navigation prop in any of your components, you may use the withNavigation HOC.
Please pass the navigation prop from the screen where you are introducing the homeScreen. It does not have access of navigation that is why it is throwing the error.