Component Exception in React Native application - react-native

LoginScreen.js
import React, { Component } from 'react'
import { Text, View, StyleSheet, TouchableOpacity, TextInput } from 'react-native'
import { color } from 'react-native-reanimated'
import Button from 'react-native-elements'
import ValidationComponent from "react-native-form-validator"
import axios from 'axios'
import AsyncStorage from '#react-native-community/async-storage'
export default class LoginScreen extends ValidationComponent {
render() {
return (
<View style = {{
width:'100%',
height:'100%',
justifyContent: 'center',
alignSelf: 'center',
alignContent: 'center',
alignItems: 'center' }}>
<Text style={styles.TextLogin}>DEW WATER</Text>
<TextInput placeholder={'Enter the user Name'}
onChangeText={(value) => this.setState({username : value})}
style={{ height:42, width:'80%',
borderBottomWidth:1 }}/>
<TextInput placeholder={'Enter the user password'}
onChangeText={(value) => this.setState({password : value})}
style={{ height:42, width:'80%',
borderBottomWidth:1, marginTop: '5%'}}/>
<View style={{
marginTop:'10%',
width:'80%'}}>
<Button titleStyle={{fontSize: 22,}} buttonStyle={{borderRadius:50,width:100,backgroundColor:'#307cee',marginBottom:5 }}
onPress={this._onPressButton} color='white' title='LoginScreen'></Button>
<View style={styles.Signup}>
<Text style={[styles.textSignup, {color: 'gray'}]}>Don't have an account?</Text>
<TouchableOpacity
onPress = {() => {this.props.navigation.replace("RegisterScreen")}}>
<Text style={[styles.textSignup, {color:'#3465d9', marginLeft: 4}]}>Signup</Text>
</TouchableOpacity>
</View>
</View>
</View>
)
}
}
const styles =StyleSheet.create({
TextLogin: {
fontSize: 35,
marginBottom: 60,
opacity: 0.9,
color:'#5f9ea0'
},
Signup: {
marginTop: 25,
flexDirection: 'row',
justifyContent: 'center'
},
textSignup: {
textAlign: 'center'
}
})
App.js
import React from 'react'
import { NavigationContainer } from '#react-navigation/native'
import { createStackNavigator } from '#react-navigation/stack'
import LoginScreen from './screens/LoginScreen'
import RegisterScreen from './screens/RegisterScreen'
import Template from './screens/Page/Template'
const Stack = createStackNavigator();
function App() {
return(
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name = 'Login' component = {LoginScreen}/>
<Stack.Screen name = 'Register' component = {RegisterScreen}/>
<Stack.Screen name = 'Template' component = {Template}/>
</Stack.Navigator>
</NavigationContainer>
)
}
export default App;

Related

React Native I can only navigate on the main screen, cannot navigate on other screens

I am just starting with React, I spent some days learning about it. I want to navigate my app normally before futher studying, I hope someone can correct my code. I am not achieving what I want.
I have this on my App.js
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View } from 'react-native';
import { colors } from './src/utils/colors';
import { Telefone } from './src/telefone';
import { Menu } from './src/menu';
import { NavigationContainer } from '#react-navigation/native';
import { createBottomTabNavigator } from '#react-navigation/bottom-tabs';
import { Procurar } from './src/procurar';
import PassageiroFrm from './src/PassageiroFrm';
function HomeScreen() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Home!</Text>
</View>
);
}
function ProcurarScreen() {
return (
<Procurar></Procurar>
);
}
function PassageiroScreen(navigation) {
return (
<PassageiroFrm></PassageiroFrm>
);
}
function SettingsScreen() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Settings!</Text>
</View>
);
}
function LoginScreen() {
return (
<Telefone></Telefone>
);
}
const Tab = createBottomTabNavigator();
export default function App({navigation}) {
return (
<View style={styles.container}>
<NavigationContainer>
<Tab.Navigator>
<Tab.Screen name="login" component={LoginScreen} />
<Tab.Screen name="cadastro" component={PassageiroScreen} />
<Tab.Screen name="início" component={ProcurarScreen} />
<Tab.Screen name="corridas" component={SettingsScreen} />
</Tab.Navigator>
</NavigationContainer>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: colors.azul,
},
});
I can navigate fine from this screen. But how about navigation on other screens.
This component doesn't navigate to "cadastro" when the button is clicked.
import React from 'react';
import {View, Text, StyleSheet, Image} from 'react-native';
import {TextInput} from 'react-native-paper';
import { colors } from './utils/colors';
import { Button } from 'react-native';
import PassageiroFrm from './PassageiroFrm';
import { Procurar } from './procurar';
import { Link } from 'react-router-dom';
import { useNavigation } from '#react-navigation/native';
let i = 0;
export const Telefone = ( {}) => (
<View style={StyleSheet.container}>
<View style={{ justifyContent :'center', alignItems: 'center'}} >
<Image source={ require("./imagens/placa.jpg")}></Image>
</View>
<TextInput label="Entre com o seu telefone"/>
<Button color="#F0C228"
style={{ fontColor:"red", buttonText: {
color: "black",
} }} onPress={() => regiao()} title="Entrar"> </Button>
<Text>Ainda não sou passageiro</Text>
<Button color="#F0C228" onPress={() => navigation.navigate('cadastro') } title="ser um passageiro"> </Button>
</View>
)
function ProcurarScreen() {
const navigation = useNavigation();
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
text: {
color: colors.branco
}
})
I would try the following in your Telefone component:
import React from 'react';
import {View, Text, StyleSheet, Image} from 'react-native';
import {TextInput} from 'react-native-paper';
import {colors} from './utils/colors';
import {Button} from 'react-native';
import PassageiroFrm from './PassageiroFrm';
import {Procurar} from './procurar';
import {Link} from 'react-router-dom';
import {useNavigation} from '#react-navigation/native';
let i = 0;
export const Telefone = () => {
const navigation = useNavigation();
return (
<>
<View style={StyleSheet.container}>
<View style={{justifyContent: 'center', alignItems: 'center'}}>
<Image source={require('./imagens/placa.jpg')}></Image>
</View>
<TextInput label="Entre com o seu telefone" />
<Button
color="#F0C228"
style={{
fontColor: 'red',
buttonText: {
color: 'black',
},
}}
onPress={() => regiao()}
title="Entrar">
{' '}
</Button>
<Text>Ainda não sou passageiro</Text>
<Button color="#F0C228" onPress={() => navigation.navigate('cadastro')} title="ser um passageiro">
{' '}
</Button>
</View>
</>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
},
text: {
color: colors.branco,
},
});
So basically, I moved const navigation = useNavigation(); inside Telefone component.
Good luck!
Actually I changed this
<Tab.Screen name="login" component={LoginScreen} />
to:
<Tab.Screen name="cadastro" component={PassageiroFrm} /> //Component itself not a function
so the navigation was available on the component
export const Telefone = ({navigation}) => {
return (
<>
<View style={StyleSheet.container}>
<View style={{justifyContent: 'center', alignItems: 'center'}}>
<Image source={require('./imagens/taxi_placa.jpg')}></Image>
</View>
<TextInput label="Entre com o seu telefone" />
<Button
color="#F0C228"
style={{
fontColor: 'red',
buttonText: {
color: 'black',
},
}}
onPress={() => regiao()}
title="Entrar">
{' '}
</Button>
<Text>Ainda não sou passageiro</Text>
<Button color="#F0C228" onPress={() => navigation.navigate('cadastro')} title="ser um passageiro">
{' '}
</Button>
</View>
</>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
},
text: {
color: colors.branco,
},
});

Element type invalid: expected sting / Check the render method of 'Onboarding Screen'?

Does anyone know why I'm getting this error? I'm passing OnboardingScreen.js to AuthStack.js, then finally to App.js. I'm really new to all this would greatly appreciate your help!
I'm trying to create a navigation flow that starts with the log in screen then later goes into the main Home Screen of the app.
import {View, Text, SafeAreaView, TouchableOpacity, Image} from 'react-native';
import React from 'react';
import MaterialIcons from 'react-native-vector-icons';
const OnboardingScreen = ({navigation}) => {
return (
<SafeAreaView
style={{
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#ffff',
}}>
<View style={{marginTop: 20}}>
<Text style={{fontSize: 30, fontWeight: 'bold', color: '#0c5ea3'}}>
ONBOARDING SCREEN
</Text>
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<Image
style={{
width: 100,
height: 100,
alignContent: 'center',
}}
source={require('../assets/images/setwork5-ICON-p-800.png')}
/>
</View>
<TouchableOpacity
onPress={() => navigation.navigate('Login')}
style={{
backgroundColor: '#0c5ea3',
padding: 20,
borderRadius: 8,
flexDirection: 'row',
justifyContent: 'space-between',
marginBottom: 50,
}}>
<Text style={{color: '#fff', fontFamily: 'Kanit-Medium'}}>
Let's Begin
</Text>
<MaterialIcons
name="arrow-forward-ios"
size={22}
color="#fff"></MaterialIcons>
</TouchableOpacity>
</View>
</SafeAreaView>
);
};
export default OnboardingScreen;
Then here I've passed into the Auth Stack...
import {View, Text} from 'react-native';
import React from 'react';
import {createNativeStackNavigator} from '#react-navigation/native-stack';
import LoginScreen from '../screens/LoginScreen';
import OnboardingScreen from '../screens/OnboardingScreen';
const Stack = createNativeStackNavigator();
const AuthStack = () => {
return (
<Stack.Navigator screenOptions={{headerShown: false}}>
<Stack.Screen name="OnboardingScreen" component={OnboardingScreen} />
<Stack.Screen name="Login" component={LoginScreen} />
</Stack.Navigator>
);
};
export default AuthStack;
Then finally into the App.js ...
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* #format
* #flow strict-local
*/
import * as React from 'react';
import type {Node} from 'react';
import {SafeAreaView, View, Text, TouchableOpacity, Image} from 'react-native';
import MaterialIcons from 'react-native-vector-icons/MaterialIcons';
import {NavigationContainer} from '#react-navigation/native';
import {createNativeStackNavigator} from '#react-navigation/native-stack';
import HomeScreen from './screens/HomeScreen';
import OnboardingScreen from './screens/OnboardingScreen';
import AuthStack from './navigation/AuthStack';
import AppStack from './navigation/AppStack';
const Stack = createNativeStackNavigator();
function App() {
return (
<NavigationContainer>
<AuthStack />
{/* <AppStack /> */}
</NavigationContainer>
);
}
// const Home = () => {
// return (
// <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
// <Text>HOME SCRRENENENE</Text>
// </View>
// );
// };
export default App;

Add openDrawer() to hamburger menu on a header in React Native -gives error undefined

This is my first React Native app. I want to get the header hamburger icon to open the side bar (drawer) but it only read it as undefined. So I read that it´s not possible to implement openDrawer() on a header as it is not read as a "screen". I have not been able to understand how to change my code to make it work. Help appreciated.
My code:
//Header
import React from "react";
import { StyleSheet, View, Text, TouchableOpacity } from "react-native";
import { Ionicons } from "#expo/vector-icons";
export const Header = ({ navigation, title }) => {
const openMenu = () => {
navigation.openDrawer();
};
return (
<View style={styles.header}>
<TouchableOpacity onPress={openMenu} style={styles.icons}>
<Ionicons name="md-menu" size={28} color="white" />
</TouchableOpacity>
<View style={styles.headerTitle}>
<Text style={styles.headerText}>{title}Title for header</Text>
</View>
</View>
);
};
//Navigation screen
import React from "react";
import { NavigationContainer } from "#react-navigation/native";
import { createDrawerNavigator } from "#react-navigation/drawer";
import { Notifications } from "./screens/Notifications";
import { Profile } from "./screens/Profile";
import { Header } from "./screens/Header";
const Drawer = createDrawerNavigator();
export const Navigation = () => {
return (
<>
<Header />
<NavigationContainer>
<Drawer.Navigator>
<Drawer.Screen name="Login" component={Login} />
<Drawer.Screen name="Profile" component={Profile} />
<Drawer.Screen name="Notifications" component={Notifications} />
</Drawer.Navigator>
</NavigationContainer>
</>
);
};
Firstly, create a folder called navigation where your App.js is located
Inside navigation folder create a file called AppNavigator.js
Then paste this inside AppNavigator.js
import React from "react";
import { createDrawerNavigator } from "#react-navigation/drawer";
import { Notifications } from "./screens/Notifications";
import { Profile } from "./screens/Profile";
const Drawer = createDrawerNavigator();
function AppNavigator() {
return (
<Drawer.Navigator screenOptions={{ headerShown: true }}>
<Drawer.Screen name="Profile" component={Profile} />
<Drawer.Screen name="Notifications" component={Notifications} />
</Drawer.Navigator>
);
}
export default AppNavigator;
In your App.js paste this code
import React from "react";
import { NavigationContainer } from "#react-navigation/native";
import AppNavigator from "./navigation/AppNavigator"
const App = () => {
return (
<NavigationContainer>
<AppNavigator />
</NavigationContainer>
);
};
export default App;
Your Login.js would look like this
import React, { useState } from "react";
import {
TouchableOpacity,
TextInput,
Text,
View,
StyleSheet,
} from "react-native";
export const Login = ({ navigation }) => {
const [userName, setUserName] = useState("");
const [password, setPassword] = useState("");
return (
<View style={styles.container}>
<Text style={styles.welcomeText}>Welcome to your App</Text>
<View style={styles.inputView}>
<TextInput
// required
style={styles.inputText}
placeholder="Email"
placeholderTextColor="#fff"
onChangeText={(value) => setUserName(value)}
defaultValue={userName}
/>
</View>
<View style={styles.inputView}>
<TextInput
// required
style={styles.inputText}
placeholder="Password"
placeholderTextColor="#fff"
onChangeText={(value) => setPassword(value)}
defaultValue={password}
/>
</View>
<TouchableOpacity>
<Text style={styles.forgot}>Forgot Password?</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.loginButton}
title="Login"
onPress={() => navigation.navigate("NavigationTest", { userName })}
>
<Text style={styles.loginText}>LOGIN</Text>
</TouchableOpacity>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center",
},
welcomeText: {
color: "#fb5b5a",
fontWeight: "bold",
fontSize: 50,
textAlign: "center",
margin: 40,
},
inputView: {
width: "80%",
backgroundColor: "#465880",
borderRadius: 25,
height: 50,
marginBottom: 20,
justifyContent: "center",
padding: 20,
},
inputText: {
height: 50,
color: "white",
},
loginButton: {
width: "80%",
backgroundColor: "#fb5b5a",
borderRadius: 25,
height: 50,
alignItems: "center",
justifyContent: "center",
marginTop: 40,
marginBottom: 10,
},
forgot: {
color: "grey",
},
loginText: {
color: "#ffff",
fontWeight: "bold",
},
});

getting white space at top when using react-native navigation

I am using react navigation to navigate between different screens.
below is my code:
import React from 'react';
import { StyleSheet, Text, View, Button, TouchableOpacity } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
import Home from "./screen/Home";
function Main({ navigation }) {
return (
<View style={styles.container}>
<View style={{flex: 0}}>
<Text>Welcome</Text>
</View>
<View style={{flex: 0}}>
<TouchableOpacity style={styles.btn} onPress={() => navigation.navigate('Home')}>
<Text>Goto Home</Text>
</TouchableOpacity>
</View>
</View>
);
}
const MainNavigator = createStackNavigator();
export default class App extends React.Component {
render() {
return (
<NavigationContainer>
<MainNavigator.Navigator >
<MainNavigator.Screen name="Main" component={Main} />
<MainNavigator.Screen name="Home" component={Home} />
</MainNavigator.Navigator>
</NavigationContainer>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1, backgroundColor: '#44210D', alignItems: 'center', justifyContent: 'center',
},
text: {
color: "black", borderWidth: 2, borderColor: "green"
},
btn: {
alignItems: "center", backgroundColor: "#DDDDDD", padding: 10,margin: 10
},
});
You can see in the image that there is a white space at the top. I want to know how can I change the color of that space.

How to navigate to other page after click on button in react native?

I am an beginner in react native.I want to display home page after logged in.I have install react-navigator using npm install and I have tried below code.
But I am getting below error, I tried to solve this. Still I have the error.
In App.js
//This is an example code for Bottom Navigation//
import React, { Component } from 'react';
//import react in our code.
import { Text, View, TouchableOpacity, StyleSheet } from 'react-native';
//import all the basic component we have used
import { createStackNavigator, createAppContainer } from 'react-navigation';
export default class App extends Component {
render() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text style={{ marginTop: 50, fontSize: 25 }}>Setting!</Text>
<View
style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<TouchableOpacity
style={styles.button}
onPress={() => this.props.navigation.navigate('Two')}>
<Text>Go to Home Tab</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => this.props.navigation.navigate('Two')}>
<Text>Open Detail Screen</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => this.props.navigation.navigate('Two')}>
<Text>Open Profile Screen</Text>
</TouchableOpacity>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
button: {
alignItems: 'center',
backgroundColor: '#DDDDDD',
padding: 10,
width: 300,
marginTop: 16,
},
});
Can anyone assist me to solve this?
You can do it as following:
import React from "react";
import { View, Text } from "react-native";
class HomeScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
<Text>Home Screen</Text>
</View>
);
}
}
class LoginScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
<Text>Login Screen</Text>
<Button
title="Go to Home"
onPress={() => this.props.navigation.navigate('Home')}
/>
</View>
);
}
}
on App.js,
import React from 'react';
import { HomeScreen, LoginScreen } from '..'// should set the proper path.
const AppNavigator = createStackNavigator({
Login: LoginScreen,
Home: HomeScreen
});
const App = () => {
return <AppNavigator />;
}
export default App;