ReferenceError:Can't find variable:navigation - react-native

I just starting using react-navigation. I also using expo. when i install react-navigation with npm it processed with no error but when i run my project by expo it will cause error with this message : ReferenceError: Can't find variable:navigation. i tried remove node_modules, clear the cache, install it with yarn, using other version but non of them solve the problem. is there any body that can help me??!!
enter image description here
import React from 'react';
import { StyleSheet, Text, Image,View,ImageBackground,TouchableHighlight }
from 'react-native';
import { Ionicons } from '#expo/vector-icons'; // 6.2.2
import { StackNavigator } from 'react-navigation';
export default class app extends React.Component
{
render() {
return <RootStack />;
}
}
class main extends React.Component {
render() {
return (
<ImageBackground source={require('./assets/img/main.jpg')} style={{width:'100%',height:'100%',flex:1}} >
<View style={{width:'100%',height:'15%'}}></View>
<View style={{flex:1,flexDirection:'row',justifyContent:'space-around'}}>
<TouchableHighlight onPress={() => navigation.navigate('Patients')}>
<View style={styles.container}>
<Text style={{padding:5,alignItems: 'center'}}>sss</Text>
</View>
</TouchableHighlight>
<TouchableHighlight onPress={navigation.navigate('Appointment')}>
<View style={styles.container}>
<Ionicons name="md-checkmark-circle" size={64} color="black" />
<Text style={{padding:5,alignItems: 'center'}}>fuck </Text>
</View>
</TouchableHighlight>
</View>
</ImageBackground>
);
}
}
class patinets extends React.Component{
render(){
return(
<View>
<Text>patient</Text>
</View>
);
}
}
class appointment extends React.Component{
render(){
return(
<View>
<Text>appointment</Text>
</View>
);
}
}
class setting extends React.Component{
render(){
return(
<View>
<Text>setting</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
borderWidth: 0,
borderRadius: 15,
borderColor: '#ddd',
borderBottomWidth: 0,
shadowColor: '#353535',
shadowOffset: { width: 0, height: 0 },
shadowOpacity: 0.1,
shadowRadius: 5,
elevation: 0.5,
marginLeft: 5,
marginRight: 5,
marginTop: 80,
padding:5,
width:'38%',
height:'20%',
justifyContent:'center',
alignItems:'center'
},
});
const RootStack = StackNavigator(
{
Main: {
screen: main,
},
Patients: {
screen: patinets,
},
Appointment: {
screen: appointment,
},
Setting: {
screen: setting,
},
},
{
mode: 'modal',
headerMode: 'none',
}
);
const MainStack = StackNavigator(
{
Home: {
screen: main,
},
Details: {
screen: main,
},
},
{
initialRouteName: 'Home',
navigationOptions: {
headerStyle: {
backgroundColor: '#f4511e',
},
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
},
},
}
);

render() {
const { navigation } = this.props.navigation;
return (
<ImageBackground source={require('./assets/img/main.jpg')} style={{width:'100%',height:'100%',flex:1}} >
<View style={{width:'100%',height:'15%'}}></View>
<View style={{flex:1,flexDirection:'row',justifyContent:'space-around'}}>
<TouchableHighlight onPress={() => navigation.navigate('Patients')}>
<View style={styles.container}>
<Text style={{padding:5,alignItems: 'center'}}>sss</Text>
</View>
</TouchableHighlight>
<TouchableHighlight onPress={navigation.navigate('Appointment')}>
<View style={styles.container}>
<Ionicons name="md-checkmark-circle" size={64} color="black" />
<Text style={{padding:5,alignItems: 'center'}}>fuck </Text>
</View>
</TouchableHighlight>
</View>
</ImageBackground>
);
}
i hope it solve

Related

ReactNative issue with contructor saying ';' error

This is my code want to implement an add country and region drop down. I am facing and error at 7,8 line saying semicolon is missing. I would like help as i am beginner at reactnative.
import React,{ Component } from 'react';
import { CountryDropdown, RegionDropdown } from 'react-country-region-selector';
import { StyleSheet, Text, TextInput, View } from 'react-native';
export default function App extends Component() {
constructor(props) {
super(props);
this.state = { country: '', region: '' };
};
selectCountry=(val)=>{
this.setState({ country: val });
};
selectRegion=(val)=>{
this.setState({ region: val });
};
const { country, region } = this.state;
return (
<View style={styles.container}>
<View style={styles.line}>
<Text style={styles.text}>Enter name:</Text>
<TextInput
style={styles.input}
placeholder='e.g. John Doe'
/>
</View>
<View style={styles.line}>
<Text style={styles.text}>Enter Age:</Text>
<TextInput
keyboardType='numeric'
style={styles.input}
placeholder='2'
/>
</View>
<View style={styles.line}>
<Text style={styles.text}>Enter Country:</Text>
<CountryDropdown
value={country}
onValueChange={(val) => this.selectCountry(val)} />
</View>
<View style={styles.line}>
<Text style={styles.text}>Enter Region:</Text>
<RegionDropdown
country={country}
value={region}
onValueChange={(val) => this.selectRegion(val)} />
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex:1,
backgroundColor: '#fff',
alignItems:'center',
justifyContent:'center',
},
line:{
flexDirection:'row',
flex:0,
marginBottom:5,
},
text:{
flex:1,
},
input:{
flex:1,
width:70,
borderBottomWidth:1,
borderBottomColor:'#777',
},
});
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
You are using a class component but calling it a function ....
change export default function App extends Component() { to export default class App extends Component(){
also add a render() function with appropriate surrounding brackets
new code below
import React,{ Component } from 'react';
import { CountryDropdown, RegionDropdown } from 'react-country-region-selector';
import { StyleSheet, Text, TextInput, View } from 'react-native';
export default class App extends Component() { <-- change here
constructor(props) {
super(props);
this.state = { country: '', region: '' };
};
selectCountry=(val)=>{
this.setState({ country: val });
};
selectRegion=(val)=>{
this.setState({ region: val });
};
render() { <-- add here
const {country, region} = this.state;
return (
<View style={styles.container}>
<View style={styles.line}>
<Text style={styles.text}>Enter name:</Text>
<TextInput
style={styles.input}
placeholder='e.g. John Doe'
/>
</View>
<View style={styles.line}>
<Text style={styles.text}>Enter Age:</Text>
<TextInput
keyboardType='numeric'
style={styles.input}
placeholder='2'
/>
</View>
<View style={styles.line}>
<Text style={styles.text}>Enter Country:</Text>
<CountryDropdown
value={country}
onValueChange={(val) => this.selectCountry(val)}/>
</View>
<View style={styles.line}>
<Text style={styles.text}>Enter Region:</Text>
<RegionDropdown
country={country}
value={region}
onValueChange={(val) => this.selectRegion(val)}/>
</View>
</View>
);
} <-- add here
}
const styles = StyleSheet.create({
container: {
flex:1,
backgroundColor: '#fff',
alignItems:'center',
justifyContent:'center',
},
line:{
flexDirection:'row',
flex:0,
marginBottom:5,
},
text:{
flex:1,
},
input:{
flex:1,
width:70,
borderBottomWidth:1,
borderBottomColor:'#777',
},
});

undefined is not an object (evaluating '_this.props.navigation') Expo React Native

I am getting undefined is not an object evaluating _this.props.navigation. Here is my code. I want to use Flatlist to call the screen 'PlaceorderScreen.js' number of times from the component 'NearbyLSP.js' present in the 'HomeScreen.js'. Also I had referred many solutions for the above problem but yet I didn't get how to get through it. I am a beginner in react-native, So I had used expo for app development.
HomeScreen.js
import React from "react";
import {
View,
Text,
StyleSheet,
SafeAreaView,
TextInput,
Platform,
StatusBar,
ScrollView,
TouchableOpacity,
Dimensions,
Animated,
} from "react-native";
import Icon from 'react-native-vector-icons/Ionicons'
import Category from './Category'
import Expressmode from './Expressmode'
import { FlatList } from "react-native-gesture-handler";
import NearbyLSP from "./NearbyLSP";
const { width } = Dimensions.get('window')
const data = [
{id:'1',name:'LSP1',phone:'1234',email:'abc#gmail.com'},
{id:'2',name:'LSP2',phone:'0234',email:'abc2#gmail.com'},
{id:'3',name:'LSP3',phone:'2234',email:'abc3#gmail.com'}
]
const renderList = ((item) =>{
return <TouchableOpacity
onPress={()=> this.props.navigation.navigate('PlaceOrder')}
activeOpacity={0.7}>
<NearbyLSP name={item.name}/>
</TouchableOpacity>
})
export default class HomeScreen extends React.Component {
componentDidMount() {
this.scrollY = new Animated.Value(0)
this.startHeaderHeight = 80
this.endHeaderHeight = 50
if (Platform.OS == 'android') {
this.startHeaderHeight = 100 + StatusBar.currentHeight
this.endHeaderHeight = 70 + StatusBar.currentHeight
}
this.animatedHeaderHeight = this.scrollY.interpolate({
inputRange: [0, 50],
outputRange: [this.startHeaderHeight, this.endHeaderHeight],
extrapolate: 'clamp'
})
this.animatedOpacity = this.animatedHeaderHeight.interpolate({
inputRange: [this.endHeaderHeight, this.startHeaderHeight],
outputRange: [0, 1],
extrapolate: 'clamp'
})
this.animatedTagTop = this.animatedHeaderHeight.interpolate({
inputRange: [this.endHeaderHeight, this.startHeaderHeight],
outputRange: [-30, 10],
extrapolate: 'clamp'
})
this.animatedMarginTop = this.animatedHeaderHeight.interpolate({
inputRange: [this.endHeaderHeight, this.startHeaderHeight],
outputRange: [50, 30],
extrapolate: 'clamp'
})
}
render() {
return (
<SafeAreaView style={{ flex: 1 }}>
<View style={{ flex: 1 }}>
<Animated.View style={{ height: this.animatedHeaderHeight, backgroundColor: 'white', borderBottomWidth: 1, borderBottomColor: '#dddddd' }}>
<View style={{
flexDirection: 'row', padding: 10,
backgroundColor: 'white', marginHorizontal: 10,
shadowOffset: { width: 0, height: 0 },
shadowColor: 'black',
shadowOpacity: 0.2,
elevation: 1,
marginTop: null
}}>
<Icon name="ios-search" size={25} style={{ marginRight: 10 ,marginTop:5 , opacity:0.6}} />
<TextInput
underlineColorAndroid="transparent"
placeholder="Find LSP"
placeholderTextColor="grey"
style={{ flex: 1, fontWeight: '700', backgroundColor: 'white' }}
/>
</View>
</Animated.View>
<ScrollView
scrollEventThrottle={16}
onScroll={Animated.event(
[
{ nativeEvent: { contentOffset: { y: this.scrollY } } }
]
)}
>
<View style={{ flex: 1, backgroundColor: 'white', paddingTop: 20 }}>
<Text style={styles.text}>
Laundry Service Provider Type
</Text>
<View style={{ height: 130, marginTop: 20 }}>
<ScrollView
horizontal={true}
showsHorizontalScrollIndicator={false}
>
<TouchableOpacity onPress={()=> this.props.navigation.navigate('PlaceOrder')} activeOpacity={0.7}><Category name="Dry Clean" /></TouchableOpacity>
<TouchableOpacity onPress={()=> this.props.navigation.navigate('PlaceOrder')} activeOpacity={0.7}><Category name="Iron"/></TouchableOpacity>
<TouchableOpacity onPress={()=> this.props.navigation.navigate('PlaceOrder')} activeOpacity={0.7}><Category name="Dry Clean + Iron" /></TouchableOpacity>
</ScrollView>
</View>
<View style={{ marginTop: 40}}>
<Text style={styles.text}>
Find Nearby LSP
</Text>
<Text style={{ fontWeight: '100', marginTop: 10,paddingHorizontal: 20 }}>
Verified LSP Providers
</Text>
<View style={{ marginTop: 20, paddingHorizontal: 15
}}>
<FlatList
data={data}
renderItem={({item})=>{
return renderList(item)
}}
keyExtractor={item=>item.id}
navigation={this.props.navigation}
/>
</View>
</View>
</View>
<View style={{ marginTop: 40 }}>
<Text style={styles.text}>
Express Mode
</Text>
<View style={{ paddingHorizontal: 20, marginTop: 20, flexDirection: 'row', flexWrap: 'wrap', justifyContent: 'space-between' }}>
<Expressmode width={width}
name="LSP1"
type="Dry Clean"
price={82}
rating={4}
/>
<Expressmode width={width}
name="LSP2"
type="Iron + Dry Clean"
price={82}
rating={4}
/>
<Expressmode width={width}
name="LSP3"
type="Iron"
price={82}
rating={4}
/>
</View>
</View>
</ScrollView>
</View>
</SafeAreaView>
);
;
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center"
},
text: {
fontSize: 24,
fontWeight:'700',
paddingHorizontal: 20
}
});
'HomeScreen.js' contains another component where I had successfully used the navigation to navigate to 'PlaceorderScreen.js'. But when I call this navigation inside Flatlist it gives the error.
NearbyLSP.js
import React from "react";
import { View, Text, StyleSheet, TouchableOpacity, Dimensions, Image } from "react-native";
import { Card } from "react-native-paper" ;
const { width } = Dimensions.get('window')
export default class NearbyLSP extends React.Component {
render() {
return (
<View>
<Card style={styles.mycard}>
<View style={{ width: width - 40, height: 200}} >
<Image
style={{ flex: 1, height: null, width: null, resizeMode: 'cover', borderRadius: 5, borderWidth: 1, borderColor: '#dddddd' }}
source={require('../assets/a.jpeg')}
/>
<Text>{this.props.name}</Text>
</View>
</Card>
</View>
);
}
}
const styles = StyleSheet.create({
mycard: {
margin: 5
}
});
App.js
import React from "react";
import { createAppContainer, createSwitchNavigator , createBottomTabNavigator } from "react-navigation";
import { createStackNavigator } from "react-navigation-stack";
import LoadingScreen from "./screens/LoadingScreen";
import LoginScreen from "./screens/LoginScreen";
import SignupScreen from "./screens/SignupScreen";
import HomeScreen from "./screens/HomeScreen";
import AboutScreen from "./screens/AboutScreen";
import PastorderScreen from "./screens/PastorderScreen";
import TrackScreen from "./screens/TrackScreen";
import ProfileScreen from "./screens/ProfileScreen";
import PlaceorderScreen from "./screens/PlaceorderScreen";
import MapsScreen from "./screens/MapsScreen";
import NearbyLSP from "./screens/NearbyLSP";
import { Ionicons , MaterialIcons } from '#expo/vector-icons';
import * as firebase from "firebase";
const firebaseConfig = {
apiKey: "AIzaSyAhwjD3jodBg6JGJ7Oo72r7-m2T5wwPaUg",
authDomain: "a4project-2d896.firebaseapp.com",
databaseURL: "https://a4project-2d896.firebaseio.com",
projectId: "a4project-2d896",
storageBucket: "a4project-2d896.appspot.com",
messagingSenderId: "181932238433",
appId: "1:181932238433:web:aa3f6fa3947976a8352c20",
measurementId: "G-SW6N6N8JYK"
};
firebase.initializeApp(firebaseConfig);
const HomeStack = createStackNavigator({
Home: HomeScreen,
NearbyLSP:NearbyLSP,
PlaceOrder:PlaceorderScreen
},
{
defaultNavigationOptions: {
title:'Home',
headerStyle: {
backgroundColor:'#12B2C2',
},
headerTintColor:'#fff',
headerTitleStyle:{
fontWeight:'bold'
}
}
})
const OrderStack = createStackNavigator({
Order: TrackScreen,
Maps: MapsScreen,
},
{
defaultNavigationOptions: {
title:'Orders',
headerStyle: {
backgroundColor:'#12B2C2',
},
headerTintColor:'#fff',
headerTitleStyle:{
fontWeight:'bold'
}
}
})
const PastorderStack = createStackNavigator({
Pastorder: PastorderScreen,
},
{
defaultNavigationOptions: {
title:'Pastorder',
headerStyle: {
backgroundColor:'#12B2C2',
},
headerTintColor:'#fff',
headerTitleStyle:{
fontWeight:'bold'
}
}
})
const ProfileStack = createStackNavigator({
Profile: ProfileScreen,
About: AboutScreen
},
{
defaultNavigationOptions: {
title:'Profile',
headerStyle: {
backgroundColor:'#12B2C2',
},
headerTintColor:'#fff',
headerTitleStyle:{
fontWeight:'bold'
}
}
})
const AuthStack = createStackNavigator({
Login: LoginScreen,
Signup: SignupScreen
},
{
defaultNavigationOptions: {
headerStyle: {
backgroundColor:'#12B2C2',
},
headerTintColor:'#fff',
headerTitleStyle:{
fontWeight:'bold'
}
}
})
const myTabs= createBottomTabNavigator({
Home:HomeStack,
Orders:OrderStack,
Profile:ProfileStack,
Pastorder:PastorderStack
},
{
defaultNavigationOptions: ({ navigation })=> {
return {
tabBarIcon:({tintColor})=>{
const { routeName } = navigation.state;
let myicon
if(routeName=="Home"){
myicon='md-home'
return <Ionicons name={myicon} size={30} color={tintColor}/>
}else if(routeName=="Profile"){
myicon='md-person'
return <Ionicons name={myicon} size={30} color={tintColor}/>
}else if(routeName=="Orders"){
myicon='md-book'
return <Ionicons name={myicon} size={30} color={tintColor}/>
}else if(routeName=="Pastorder"){
myicon='library-books'
return <MaterialIcons name={myicon} size={30} color={tintColor}/>
}
}
};
}
});
export default createAppContainer(
createSwitchNavigator(
{
Loading: LoadingScreen,
Auth: AuthStack,
App: myTabs
},
{
initialRouteName: "Loading"
},
)
);
Just writing navigation={this.props.navigation} in Flatlist won't let you navigate to the desired screen. You have to create a Touchable component in the Flatlist and add the navigation logic into the onPress logic of this Touchable Component.
this.props.navigation is a collection of data and methods, if you need to navigate through screens you need to use this.props.navigation.navigate('PlaceOrder')} instead, this is the method that does the navigation for you. You can console.log(this.props.navigation) to understand better.
Refer to how navigation is implemented in this answer https://stackoverflow.com/a/45407626/8851276
The code within FlatList of 'HomeScreen.js' is modified as follows
<FlatList
data={data}
renderItem={this.rendeList}
keyExtractor={item=>item.id}
/>
and the renderList function which was outside the class component is transferred inside and modified as
renderList = ({ item }) => {
return (
<TouchableOpacity
onPress={()=> this.props.navigation.navigate('PlaceOrder')}
activeOpacity={0.7}>
<NearbyLSP name={item.name}/>
</TouchableOpacity>
);
};
Due to transferring the renderList function inside the class component, it gets access to the this.props and it can be used for navigation to new screen.

How to show footer in full screen

I have a footer that contains several buttons that I want to show in full screen. I'm using below code:
**//App.js**
export default class App extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<View style={{flex: 1, backgroundColor: '#6ED4C8'}}>
<AppNavigator />
<View style={{flexDirection: 'row'}}>
<Footer />
</View>
</View>
);
}
}
**//AppNavigator.js**
const AppNavigator = createStackNavigator(
{
Home: {screen: Home},
Friends: {screen: Friends},
Login: {screen: Login},
PostDatiles: {screen: PostDatiles},
Aboutus: {screen: Aboutus},
Footer: {screen: Footer},
},
{
headerMode: 'none',
navigationOptions: {
headerVisible: false,
},
// ,initialRouteName: ''
},
);
export default AppNavigator;
**//Footer.js**
export default class Footer extends React.Component {
constructor(props) {
super(props);
}
render() {
const {navigation} = this.props;
return (
<View style={styles.container}>
<View>
<Button
title="About us"
color="#39F80F"
accessibilityLabel="Tap on Me"
onPress={() => navigation.navigate('Login')}
/>
</View>
<View>
<Button
title="Login"
color="#F8380F"
accessibilityLabel="Tap on Me"
/>
</View>
<View>
<Button
title="Search"
color="#B80FF8"
accessibilityLabel="Tap on Me"
/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
},
});
When I click on one of the buttons, I get this error:
enter image description here
In the app.js file I use a footer tag because I want show it on all of the screen.
I searched a lot but couldn't find any similar problem!
To do what you want to do, you must use the createBottomTabNavigator instead of the StackNavigator.
Example
import React from 'react';
import { Text, View } from 'react-native';
import { Ionicons } from '#expo/vector-icons'; // 6.2.2
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>
);
}
}
class IconWithBadge extends React.Component {
render() {
const { name, badgeCount, color, size } = this.props;
return (
<View style={{ width: 24, height: 24, margin: 5 }}>
<Ionicons name={name} size={size} color={color} />
{badgeCount > 0 && (
<View
style={{
// /If you're using react-native < 0.57 overflow outside of the parent
// will not work on Android, see https://git.io/fhLJ8
position: 'absolute',
right: -6,
top: -3,
backgroundColor: 'red',
borderRadius: 6,
width: 12,
height: 12,
justifyContent: 'center',
alignItems: 'center',
}}>
<Text style={{ color: 'white', fontSize: 10, fontWeight: 'bold' }}>
{badgeCount}
</Text>
</View>
)}
</View>
);
}
}
const HomeIconWithBadge = props => {
// You should pass down the badgeCount in some other ways like context, redux, mobx or event emitters.
return <IconWithBadge {...props} badgeCount={3} />;
};
const getTabBarIcon = (navigation, focused, tintColor) => {
const { routeName } = navigation.state;
let IconComponent = Ionicons;
let iconName;
if (routeName === 'Home') {
iconName = `ios-information-circle${focused ? '' : '-outline'}`;
// We want to add badges to home tab icon
IconComponent = HomeIconWithBadge;
} else if (routeName === 'Settings') {
iconName = `ios-options${focused ? '' : '-outline'}`;
}
// You can return any component that you like here!
return <IconComponent name={iconName} size={25} color={tintColor} />;
};
export default createAppContainer(
createBottomTabNavigator(
{
Home: { screen: HomeScreen },
Settings: { screen: SettingsScreen },
},
{
defaultNavigationOptions: ({ navigation }) => ({
tabBarIcon: ({ focused, tintColor }) =>
getTabBarIcon(navigation, focused, tintColor),
}),
tabBarOptions: {
activeTintColor: 'tomato',
inactiveTintColor: 'gray',
},
}
)
);

How to display both Bottom Navigator and Drawer Navigator in React Native

I am new to React Native. I want to build Bottom Navigator and Drawer Navigation. I used the below code, I am unable to navigate to the page from Drawer Menu. I am able to navigate to the page from Tab Navigator.
In the code both Drawer Navigator and Tab Navigator Screens are different.
import React, { Component } from "react";
import { StyleSheet, Text, View, TouchableOpacity } from "react-native";
import Icon from "#expo/vector-icons/Ionicons";
import {
createAppContainer,
createDrawerNavigator,
createBottomTabNavigator,
createStackNavigator
} from "react-navigation";
import { Constants } from "expo";
const DrawerMenuItems = [
{
title: "Login",
icon: "ios-home",
navigateTo: "Login"
},
{
title: "Feedback",
icon: "ios-home",
navigateTo: "Feedback"
},
{
title: "Help",
icon: "ios-help",
navigateTo: "Help"
}
];
export default class App extends React.Component {
render() {
return <AppContainer />;
}
}
class HomeScreen extends Component {
render() {
return (
<View style={styles.container}>
<Text>HomeScreen</Text>
</View>
);
}
}
class ContactScreen extends Component {
render() {
return (
<View style={styles.container}>
<Text>ContactScreen</Text>
</View>
);
}
}
class AboutScreen extends Component {
render() {
return (
<View style={styles.container}>
<Text>AboutScreen</Text>
</View>
);
}
}
class LoginScreen extends Component {
render() {
return (
<View style={styles.container}>
<Text>LoginScreen</Text>
</View>
);
}
}
class FeedbackScreen extends Component {
render() {
return (
<View style={styles.container}>
<Text>FeedbackScreen</Text>
</View>
);
}
}
class HelpScreen extends Component {
render() {
return (
<View style={styles.container}>
<Text>HelpScreen</Text>
</View>
);
}
}
class HamburgerMenu extends Component {
constructor(props) {
super(props);
}
renderMenu(menuItems) {
{
return menuItems.map((item, index) => {
return (
<View style={{ width: "100%" }} key={index}>
<View style={styles.sideMenuItem}>
<TouchableOpacity
style={styles.sideMenuIcon}
onPress={() => {
this.props.navigation.navigate(item.navigateTo);
}}
>
<Icon name={item.icon} size={30} />
</TouchableOpacity>
<Text
style={styles.menuText}
onPress={() => {
this.props.navigation.navigate(item.navigateTo);
}}
>
{item.title}
</Text>
</View>
</View>
);
});
}
}
render() {
return (
<View style={styles.sideMenuContainer}>
{this.renderMenu(DrawerMenuItems)}
</View>
);
}
}
const DrawerButton = ({ navigation }) => (
<TouchableOpacity onPress={() => navigation.openDrawer()}>
<Icon style={styles.menuIcon} name="md-menu" size={30} />
</TouchableOpacity>
);
const BottomTabsNavigator = createBottomTabNavigator(
{
Home: HomeScreen,
Contact: ContactScreen,
About: AboutScreen
},
{
tabBarOptions: {
activeTintColor: "#000",
inactiveTintColor: "gray",
style: {
backgroundColor: "#fff"
},
indicatorStyle: {
backgroundColor: "#000"
}
}
}
);
const AppDrawNavigator = createDrawerNavigator(
{
Main: { screen: BottomTabsNavigator }
},
{
contentComponent: HamburgerMenu,
drawerPosition: "right"
}
);
const AppStackNavigator = createStackNavigator(
{
MainDrawNavigator: {
screen: AppDrawNavigator
}
},
{
defaultNavigationOptions: ({ navigation }) => {
return {
header: (
<View style={styles.header}>
<DrawerButton navigation={navigation} />
</View>
),
headerStyle: {
height: 60
}
};
}
}
);
const AppContainer = createAppContainer(AppStackNavigator);
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center"
},
sideMenuContainer: {
width: "100%",
height: "100%",
backgroundColor: "#fff",
alignItems: "center",
paddingTop: 20
},
sideMenuProfileIcon: {
resizeMode: "center",
width: 150,
height: 150,
borderRadius: 150 / 2
},
sideMenuIcon: {
resizeMode: "center",
width: 28,
height: 28,
marginRight: 10,
marginLeft: 20
},
menuIcon: {
left: 10
},
menuText: {
fontSize: 15,
color: "#222222"
},
header: {
paddingTop: Constants.statusBarHeight,
backgroundColor: "#e1e8ee",
flexDirection: "row"
}
});
Kindly help on this.

Navigator with args

I created an Android app with Navigator.
On my first page, I want to push differents informations to the next page if I push with a button or with an other.
I created gotoNext function as below. But, I don't understand how I can replace my typedmg: 'test' by an argument which represent the button Pole or Aerial.
Thanks for your help.
class LoginPage extends React.Component {
render() {
return (
<Navigator
renderScene={this.renderScene.bind(this)}
navigator={this.props.navigator}
navigationBar={
<Navigator.NavigationBar routeMapper= NavigationBarRouteMapper} />
} />
);
}
renderScene(route, navigator) {
return (
....
<View style={{flex: 4, flexDirection: 'column'}}>
<TouchableHighlight name='Pole' onPress={this.gotoNext}>
<Image source={require('./img/radio_damage_pole.png')} />
</TouchableHighlight>
<TouchableHighlight name='Aerial' onPress={this.gotoNext}>
<Image source={require('./img/radio_damage_aerial.png')} />
</TouchableHighlight>
</View>
...
);
}
gotoNext() {
this.props.navigator.push({
id: 'page_user_infos',
name: 'page_user_infos',
typedmg: 'test',
});
}
}
You need to set up your Navigator to pass properties by assigning the passProps spread operator to the Navigator. The best way to do that is to separate your navigator into it's own component, then assign properties to it.
I've taken your project and set up a similar functioning one here. There was another similar thread you may be able to reference here as well. Below is the code I used to get it working, I hope this helps!
https://rnplay.org/apps/UeyIBQ
'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
Navigator,
Image,
TouchableHighlight, TouchableOpacity
} = React;
class Two extends React.Component {
render(){
return(
<View style={{marginTop:100}}>
<Text style={{fontSize:20}}>Hello From second component</Text>
<Text>id: {this.props.id}</Text>
<Text>name: {this.props.name}</Text>
<Text>name: {this.props.typedmg}</Text>
</View>
)
}
}
class Main extends React.Component {
gotoNext(myVar) {
this.props.navigator.push({
component: Two,
passProps: {
id: 'page_user_infos',
name: 'page_user_infos',
typedmg: myVar,
}
})
}
render() {
return(
<View style={{flex: 4, flexDirection: 'column', marginTop:40}}>
<TouchableHighlight style={{height:40, borderWidth:1, marginBottom:10}} name='Pole' onPress={ () => this.gotoNext('Pole') }>
<Text>Pole</Text>
</TouchableHighlight>
<TouchableHighlight style={{height:40, borderWidth:1, marginBottom:10}} name='Aerial' onPress={ () => this.gotoNext('Aerial') }>
<Text>Aerial</Text>
</TouchableHighlight>
</View>)
}
}
class App extends React.Component {
render() {
return (
<Navigator
initialRoute={{name: 'Main', component: Main, index: 0}}
renderScene={(route, navigator) => {
if (route.component) {
return React.createElement(route.component, { ...this.props, ...route.passProps, navigator, route } );
}
}}
navigationBar={
<Navigator.NavigationBar routeMapper={NavigationBarRouteMapper} />
} />
);
}
}
var NavigationBarRouteMapper = {
LeftButton(route, navigator, index, navState) {
if(index > 0) {
return (
<TouchableHighlight style={{marginTop: 10}} onPress={() => {
if (index > 0) {
navigator.pop();
}
}}>
<Text>Back</Text>
</TouchableHighlight>
)} else {
return null}
},
RightButton(route, navigator, index, navState) {
return null;
},
Title(route, navigator, index, navState) {
return (
<TouchableOpacity style={{flex: 1, justifyContent: 'center'}}>
<Text style={{color: 'white', margin: 10, fontSize: 16}}>
Data Entry
</Text>
</TouchableOpacity>
);
}
};
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 28,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
fontSize: 19,
marginBottom: 5,
},
});
AppRegistry.registerComponent('App', () => App);