I can't change the screen - react-native

I've got a component(Bottom.js) inside of another component(LoginScreen.js), inside this component(Bottom.js) there is a button(TouchableOpacity), I want to press it and change the LoginScreen to RegisterScreen, but I can't! I tried to see the docs of react navigation and I still confused. Could someone help me?
App.js
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
//import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet } from 'react-native';
import LoginScreen from './src/screens/Login/LoginScreen';
import RegisterScreen from './src/screens/Register/RegisterScreen';
import mock from './src/mocks/Login'
const Stack = createStackNavigator();
const MyTheme = {
dark: false,
colors: {
background: 'rgb(7, 19, 24)',
},
};
export default function App(){
return (
<NavigationContainer theme={MyTheme}>
<Stack.Navigator screenOptions={{headerShown: false}}>
<Stack.Screen name='Login' >
{() => <LoginScreen {...mock}/>}
</Stack.Screen>
<Stack.Screen name='Register' >
{() => <RegisterScreen />}
</Stack.Screen>
</Stack.Navigator>
</NavigationContainer>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#071318',
},
});
LoginScreen.js
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
import {Button, Input, Image} from 'react-native-elements'
import Top from './components/Top'
import Form from './components/Form'
import ButtonEnter from './components/ButtonEnter'
import Bottom from './components/Bottom'
export default function LoginScreen({top, form, buttonEnter, bottom, navigation})
{
return <>
<View style={styles.background}>
<StatusBar style='light'/>
<Top {...top}/>
<Form {...form}/>
<ButtonEnter {...buttonEnter}/>
<Bottom {...bottom} {...navigation}/>
</View>
</>
}
const styles = StyleSheet.create({
background:{
backgroundColor: "#071318"
},
text:{
color:'#AAA',
fontSize: 16,
},
logo:{
width: 160,
height: 160,
marginHorizontal: '25%',
marginTop: '15%'
}
})
Bottom.js
import React from 'react';
import { StyleSheet,View, Text, TouchableOpacity} from 'react-native';
export default function Bottom({signupButtonText, navigation})
{
return <>
<View style={styles.view}>
<TouchableOpacity style={styles.button} onPress={() => navigation.navigate('Register')}>
<Text style={styles.helpButton}>{signupButtonText}</Text>
</TouchableOpacity>
</View>
</>
}
const styles = StyleSheet.create({
view:{
flexDirection:'row',
width: '70%',
height:'40%',
marginHorizontal:'15%',
justifyContent:'center',
},
button:{
marginTop: 16,
backgroundColor: '#0B2027',
paddingVertical: 12,
borderRadius:20,
width: 80,
alignItems: 'center',
position: 'absolute',
bottom: 30
},
image:{
width:'30%',
marginHorizontal:'35%'
},
helpButton:{
color:'white',
justifyContent:'space-around'
}
})
Register.js
import React from 'react'
import { View, Text } from 'react-native'
const RegisterScreen = ({navigation}) => {
return (
<View>
<Text>register</Text>
</View>
)
}
export default RegisterScreen

Bottom component is expecting to receive Navigation, not its properties, so try this:
<Bottom navigation={navigation} {...bottom}/>

Related

The action 'NAVIGATE' with payload {"name":"AddTask"} was not handled by any navigator. Do you have a screen named 'AddTask'?

App.js
import React from'react';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator, Header } from '#react-navigation/stack';
import { createBottomTabNavigator } from '#react-navigation/bottom-tabs';
import Todo from './Todo';
import Done from './Done';
import Entrypage from './Entrypage';
import Addtask from './Addtask';
import Fontawesome5 from 'react-native-vector-icons/FontAwesome5';
import { StatusBar } from 'react-native';
const Tab = createBottomTabNavigator();
const Hometabs = () => {
return(
<NavigationContainer
independent={true}
>
<Tab.Navigator
screenOptions={({route})=>({
tabBarActiveTintColor:('#4682b4'),
tabBarLabelStyle:{fontSize:13,fontWeight:'bold'},
tabBarIcon:({focused,size,color,}) =>{
let iconname;
if(route.name==='To-do'){
iconname='clipboard-list';
color=focused? '#383':'#555';
size=focused? 26 : 25;
}else if(route.name==='Done'){
iconname='clipboard-check';
color=focused? '#383':'#555';
size=focused? 26 : 25;
}
return(
<Fontawesome5
name={iconname}
color={color}
size={size}
/>
)
}
})}
>
<Tab.Screen
name="To-do"
component={Todo}
options={{header:() => null}}
/>
<Tab.Screen
name="Done"
component={Done}
options={{header:() => null}}
/>
</Tab.Navigator>
</NavigationContainer>
)
}
const RootStack = createStackNavigator();
const App = () =>{
return(
<NavigationContainer>
<RootStack.Navigator
initialRouteName="Entry"
>
<RootStack.Screen
name="Entry"
component={Entrypage}
options={{
header:() => null
}}
/>
<RootStack.Screen
name="Tasks"
component={Hometabs}
options={{headerStyle:{backgroundColor:'#4682b4'},
headerTintColor:'#fff',
headerTitleAlign:'center',
}}
/>
<RootStack.Screen
name="AddTask"
component={Addtask}
/>
</RootStack.Navigator>
</NavigationContainer>
)
}
export default App;
Entrypage:
import React, { useEffect } from 'react';
import{
View,
Text,
Image,
StyleSheet,
StatusBar,
}from 'react-native';
export default function Enrtypage({navigation}) {
useEffect(()=>{
setTimeout(()=>{
navigation.replace('Tasks');
},2000);
},[]);
return(
<View style={styles.body}>
<StatusBar
backgroundColor={'#4682b4'}
/>
<Image
style={styles.logo}
source={require('../assests/write.png')}
/>
<Text style={styles.Text}>
To-Do List
</Text>
</View>
)
}
const styles = StyleSheet.create({
body:{
flex:1,
backgroundColor:'#4682b4',
alignItems:'center',
justifyContent:'center',
},
Text:{
fontSize: 35,
fontWeight:'900',
margin:15,
color:'#fff'
},
logo:{
height:150,
width: 150,
},
})
Todo:
import React from 'react';
import{
View,
Text,
StyleSheet,
TouchableOpacity,
Button,
}from 'react-native';
import FontAwesome5 from 'react-native-vector-icons/FontAwesome5';
export default function Todo ({navigation}) {
return(
<View style={styles.body}>
<TouchableOpacity style={styles.button}
onPress={()=>{
navigation.navigate('AddTask');
}}
>
<FontAwesome5
name='pen'
color='#fff'
size={23}
/>
</TouchableOpacity>
</View>
)
}
const styles = StyleSheet.create({
body:{
flex:1,
},
text:{
color:'#000',
},
button:{
width:60,
height:60,
backgroundColor:'#4682b4',
borderRadius: 50,
borderColor:'#000',
justifyContent:'center',
alignItems:'center',
position:'absolute',
bottom:20,
right: 20,
},
})
Done:
import React from 'react';
import{
View,
Text,
StyleSheet,
}from 'react-native';
export default function Done () {
return(
<View style={styles.body}>
<Text style={styles.text}>
Done
</Text>
</View>
)
}
const styles = StyleSheet.create({
body:{
flex:1,
},
text:{
color:'#000',
}
})
Addtask:
import React from 'react';
import{
View,
StyleSheet,
} from 'react-native';
import { TextInput } from 'react-native-gesture-handler';
export default function Addtask () {
return(
<View>
<TextInput/>
</View>
)
}
const style=StyleSheet.create({
body:{
flex:1,
},
})
The Button in Todo is not navigating to Addtask page.

How to make a bottomtabnavigator in react-native?

i have been trying to make a BottomTabNavigator for my app but has been unable to do so. Kindly help me in identifying error in the code.
This is the code written in App.js
import * as React from 'react';
import { createBottomTabNavigator } from '#react-navigation/bottom-tabs';
import mapscreen from './screens/MapScreen';
import groupscreen from './screens/GroupScreen';
import { NavigationContainer } from '#react-navigation/native';
const Tab = createBottomTabNavigator();
export default () => (
<NavigationContainer>
<Tab.Navigator>
<Tab.Screen name="map" component={mapscreen}/>
<Tab.Screen name="group" component={groupscreen} />
</Tab.Navigator>
</NavigationContainer>
);
This the code written in MapScreen.js
import * as React from 'react';
import { View, Text,StyleSheet} from 'react-native';
export const mapscreen = () => {
<View style = {styles.container}>
<Text>
Map will exist here!!
</Text>
</View>
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});
This is the code written in GroupScreen.js
import * as React from 'react';
import { View, Text,StyleSheet} from 'react-native';
export const groupscreen = () => {
<View style = {styles.container}>
<Text>
group exist here!!
</Text>
</View>
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});
Working App: Expo Snack
There were few syntactical errors, other than that everything was fine.
//App.js
import * as React from 'react';
import { createBottomTabNavigator } from '#react-navigation/bottom-tabs';
import MapsScreen from './screens/MapsScreen';
import GroupScreen from './screens/GroupScreen';
import { NavigationContainer } from '#react-navigation/native';
const Tab = createBottomTabNavigator();
export default App=() => (
<NavigationContainer>
<Tab.Navigator>
<Tab.Screen name="Map" component={MapsScreen} />
<Tab.Screen name="Group" component={GroupScreen} />
</Tab.Navigator>
</NavigationContainer>
);
//MapsScreen.js
import * as React from 'react';
import { View, Text, StyleSheet } from 'react-native';
export default MapsScreen = ({navigation}) => {
return <View style={styles.container}>
<Text>Map will exist here!!</Text>
</View>;
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});
//GroupScreen.js
import * as React from 'react';
import { View, Text, StyleSheet } from 'react-native';
export default GroupScreen = ({navigation}) => {
return <View style={styles.container}>
<Text>group exist here!!</Text>
</View>;
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});

React native: can't configure the header with navigationOptions

I am new to react-native. I am trying to configure header styles for my app, but it's not working
App.js
import { StatusBar } from 'expo-status-bar';
import React, {useState} from 'react';
import { StyleSheet, Text, View } from 'react-native';
import MealNavigator from './navigation/MealsNavigator';
export default function App() {
return (
<MealNavigator />
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
The following js file i am using for navigation
MealsNavigator.js
import { createStackNavigator } from 'react-navigation-stack';
import { createAppContainer } from "react-navigation";
import categoriesScreen from '../screens/categoriesScreen';
import categoryMealScreen from '../screens/categoryMealScreen';
import mealDetailScreen from '../screens/mealDetailScreen';
const MealNavigator = createStackNavigator({
Categories : categoriesScreen,
CategoryMeals : categoryMealScreen,
MealDetail : mealDetailScreen
});
export default createAppContainer(MealNavigator);
The following is the screen where i am trying to configure the header
categoriesScreen.js
import React from 'react';
import {Text,View,Button,FlatList,StyleSheet,TouchableOpacity, Platform} from 'react-native';
import { CATEGORIES } from '../data/dummydata';
const CategoriesScreen = props => {
const renderGrid=(itemData) =>{
return(
<TouchableOpacity style={styles.gridItem} onPress={() =>{props.navigation.navigate({routeName:'CategoryMeals'});}}>
<View>
<Text>{itemData.item.title}</Text>
</View>
</TouchableOpacity>
)
};
return(
<View style={styles.container}>
<FlatList
data={CATEGORIES} renderItem={renderGrid} numColumns={2} />
</View>
);
}
CategoriesScreen.defaultNavigationOptions = ({ navigation }) =>({
title:'Meal Categories',
headerTitleStyle: {
textAlign: "left",
fontSize: 24
},
});
const styles = StyleSheet.create({
container:{
flex:1,
justifyContent:'center',
alignItems:'center'
},
gridItem:{
flexGrow:1,
padding: 20,
margin: 15,
height: 150,
}
});
export default CategoriesScreen;
The following is the dummy data i am using
dummydata.js
import Category from '../models/category';
export const CATEGORIES = [
new Category('c1','Indian','#f5428d'),
new Category('c2','Chinese','#f54242'),
new Category('c3','Thai','#f5a442'),
new Category('c4','Malaysian','#f5d142'),
new Category('c5','Arabian','#368dff'),
new Category('c6','South Indian','#41d95d'),
new Category('c7','Kerala','#9eecff'),
new Category('c8','Bengali','#b9ffb0'),
new Category('c9','Mexican','#ffc7ff'),
new Category('c10','Italian','#47fced'),
];
Following is category class file
category.js
class Category{
constructor(id,title,color){
this.id = id;
this.title = title;
this.color = color;
}
};
export default Category;
Everything else is working, just the header configuration is not working.I am using react navigation version 4
you can cofigure header using navigation.setOptions like this:
import React from 'react';
import {Text,View,Button,FlatList,StyleSheet,TouchableOpacity, Platform} from 'react-native';
import { CATEGORIES } from '../data/dummydata';
const CategoriesScreen = props => {
React.useLayoutEffect(() => {
props.navigation.setOptions({
headerTitle: 'Meal Categories',
headerTitleStyle: {
textAlign: "left",
fontSize: 24
},
});
},
const renderGrid=(itemData) =>{
return(
<TouchableOpacity style={styles.gridItem} onPress={() =>{props.navigation.navigate({routeName:'CategoryMeals'});}}>
<View>
<Text>{itemData.item.title}</Text>
</View>
</TouchableOpacity>
)
};
return(
<View style={styles.container}>
<FlatList
data={CATEGORIES} renderItem={renderGrid} numColumns={2} />
</View>
);
}
const styles = StyleSheet.create({
container:{
flex:1,
justifyContent:'center',
alignItems:'center'
},
gridItem:{
flexGrow:1,
padding: 20,
margin: 15,
height: 150,
}
});
export default CategoriesScreen;
I found a different solution to my problem. I used defaultNavigationOptions in my navigation file.
MealsNavigator.js
import { createStackNavigator } from 'react-navigation-stack';
import { createAppContainer } from "react-navigation";
import categoriesScreen from '../screens/categoriesScreen';
import categoryMealScreen from '../screens/categoryMealScreen';
import mealDetailScreen from '../screens/mealDetailScreen';
import {Platform} from 'react-native';
import colors from '../constants/colors';
const MealNavigator = createStackNavigator({
Categories : {
screen : categoriesScreen,
navigationOptions : {
headerTitle:'Meal Categories',
}
},
CategoryMeals : {
screen : categoryMealScreen
},
MealDetail : mealDetailScreen
},
{
defaultNavigationOptions:{
headerTitleStyle:{
backgroundColor:Platform.OS==='android' ? colors.primaryColor : 'white'
},
headerTintColor:Platform.OS==='android' ? 'white' : colors.primaryColor
}
}
);
export default createAppContainer(MealNavigator);
Thanks to everybody for your help.

react native rendering useless gap(area) issue with flex

I am a newcomer for React Native with expo, so still struggling with the flex even though reading many documents. As an attached image, there is a gap, red rectangle area in the image, between header and router style in the Home component. I tried to remove that area but have no idea so far. Could you give me any advice?
Component: Home
import React, { useEffect, useState } from 'react';
import { StyleSheet, Text, View, Button, FlatList, Dimensions } from 'react-native';
import LocationBar from "./locationBar";
import { Router, Scene } from 'react-native-router-flux';
import Item from "../Item/item";
import Main from "./main";
export default function Home() {
return (
<>
<View style={styles.header}>
<LocationBar />
</View>
<Router style={styles.router}>
<Scene key="root">
<Scene key="main" component={Main} initial={true} />
<Scene key="item" component={Item} />
</Scene>
</Router>
</>
);
}
const width_proportion = '100%';
const height_proportion = '15%';
const styles = StyleSheet.create({
header: {
flex:0.15,
backgroundColor: 'yellow',
width: width_proportion,
},
router: {
flex: 1,
backgroundColor: 'blue',
width: width_proportion,
justifyContent: 'center',
alignItems: 'center'
},
});
Component: Item
import * as React from 'react';
import { View, StyleSheet, Dimensions, Text, Button, FlatList, } from 'react-native';
import { TabView, SceneMap } from 'react-native-tab-view';
const FirstRoute = () => (
<View style={[styles.scene, { backgroundColor: '#ff4081' }]} />
);
const SecondRoute = () => (
<View style={[styles.scene, { backgroundColor: '#673ab7' }]} />
);
const initialLayout = { width: Dimensions.get('window').width };
export default function Item({ menu, itemId }) {
const [index, setIndex] = React.useState(0);
const [routes] = React.useState([
{ key: 'first', title: 'First' },
{ key: 'second', title: 'Second' },
]);
const renderScene = SceneMap({
first: FirstRoute,
second: SecondRoute,
});
return (
<>
<TabView
navigationState={{ index, routes }}
renderScene={renderScene}
onIndexChange={setIndex}
initialLayout={initialLayout}
>
);
}
const styles = StyleSheet.create({
scene: {
flex: 1,
},
});
Component: Location
import React, { useEffect, useState } from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
import MaterialIcons from 'react-native-vector-icons/MaterialIcons';
import * as Location from "expo-location";
export default function LocationBar() {
const [lat, setLat] = useState();
const [lon, setLon] = useState();
useEffect(() => {
...
});
return (<View style={styles.container}>
<MaterialIcons name="location-on" size={30} />
<Text>
({lat}-{lon})
</Text>
</View>);
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
flexDirection: 'row',
marginTop: 10
},
});
I got the answer from this post! hideNavBar={true} is the key to hide that area

this.props.navigation undefined is not an object

I am new in react native. I am using React navigation. But navigation is not working, giving an error:-
"props.navigation undefined is
not an object"
I am giving my project structure and code.
Project Structure:-
Code in App.js:-
import { createStackNavigator, createDrawerNavigator } from 'react-navigation';
import HomeLayout from './src/components/Home/Layout';
import SideMenuLayout from './src/components/DrawerMenu/SideMenu';
import { DetailsScreen } from './DetailsScreen';
const myDrawer = createDrawerNavigator({
Home:{
screen: HomeLayout
},
Details:{
screen: DetailsScreen
},
},{
contentComponent: SideMenuLayout,
drawerWidth: 876/3
})
const native = createStackNavigator({
Home:{
screen: myDrawer,
},
},{
headerMode: 'none',
})
export default native;
SideMenu.js(Layout for drawer navigator). In this section All menus has been rendered.-
import React, {Component} from 'react';
import { View, StyleSheet} from 'react-native';
import DrawerHeader from './DrawerHeader';
import DrawerMenu from './DrawerMenu';
class SideMenuLayout extends Component {
state = {
menuNames:[{
id:'0',
name:'My Profile'
},{
id:'1',
name:'Place Order'
},{
id:'2',
name:'Order History'
},{
id:'3',
name:'Payment'
},{
id:'4',
name:'Recharge'
},{
id:'5',
name:'Help'
},{
id:'6',
name:'Logout'
}]
}
render () {
return (
<View style={styles.container} >
<DrawerHeader/>
<DrawerMenu menuItems={this.state} style={{ marginTop: 106/3}}/>
</View>
);
}
}
export default SideMenuLayout;
const styles = StyleSheet.create({
container:{
flex:1,
backgroundColor: "#ffffff",
}
});
Drawer Menu.js. In this section all menus are rendered and want to navigate from here.
import React, {Component} from 'react';
import {FlatList, Text, View, StyleSheet,TouchableOpacity} from 'react-native';
import Dash from 'react-native-dash';
import { FontAwesome } from '#expo/vector-icons';
import { StackNavigator } from 'react-navigation';
const DrawerMenu = (props)=>{
const { navigate } = props.navigation;
return (
<View style={styles.menuContainer}>
<FlatList
data={props.menuItems.menuNames}
renderItem={(info) => (
<View>
<View
style={{
width:690/3,
backgroundColor: '#ffffff',
height:141/3,
paddingTop:48/3,
flexDirection:'row',
marginBottom:7
}}>
<View style={{position:"relative",width:390/3,height:'100%',backgroundColor:'#ffffff',alignSelf: 'flex-start'}}>
<TouchableOpacity onPress={()=>navigate('Details')}>
<Text style={{color:'#615b5b',fontSize:18,fontFamily:'sans-serif'}}>
{info.item.name}
</Text>
</TouchableOpacity>
</View>
<View
style={{
alignItems:"center",
justifyContent:'center',
position:"relative",
width:300/3,
backgroundColor:'#ffffff',
paddingRight:10,
paddingBottom:5,
}}>
<FontAwesome
style={{ alignSelf: 'flex-end'}}
name="angle-right"
size={18}
color="#615b5b"
/>
</View>
</View>
<Dash dashColor = '#d6d6d6' style={{width:'100%', height:1,alignSelf: 'flex-start'}}/>
</View>
)}
keyExtractor={(info, index) => info.id}
/>
</View>
);
}
export default DrawerMenu;
const styles = StyleSheet.create({
menuContainer:{
width: 700/3,
height:1425/3,
alignSelf: 'flex-end',
backgroundColor: '#ffffff',
paddingTop: 61/3,
}
});
The SideMenuLayout has a navigation prop passed to it automatically by the Drawer. So you need to pass that further to your DrawerMenu
<DrawerMenu navigation={this.props.navigation} menuItems={this.state} style={{ marginTop: 106/3}}/>
...
export default SideMenuLayout;
SideMenuLayout.propTypes = {
navigation: PropTypes.object // Or use flow, or it does not really matter. It's just type validation.
}
And the you can use navigate in your DrawerMenu like you use it now.