undefined is not an object while evaluating props in react-native - react-native

So i am passing some data to the page where i am navigating on click event as
render(){
const goToPageTwo = () => Actions.gray({text:"hello"});
return(
</View>
<Button onPress={goToPageTwo} title="Checkout" color="#841584"/>
</View>
);
}
Now in my other component if i access it as props
this.props.text
It gives an exception saying props is undefined
Please guide how to resolve
Thanks!
Update:
Code which is accessing this.props
import React, { Component } from 'react';
import { Actions } from 'react-native-router-flux'; // New code
import {
StyleSheet,
Text,
Alert,
View
} from 'react-native';
const GrayScreen = () => {
return (
<View style={styles.container}>
<Text style={styles.welcome} onPress={() => Actions.black()}>{this.props.text}</Text>
</View>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'gray',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
color: 'blue',
},
});
export default GrayScreen;
code for Routing compponent
import React, { Component } from 'react';
import { Button } from 'react-native';
import Archives from './components/Archives.js';
import ScarletScreen from './components/ScarletScreen.js';
import GrayScreen from './components/GrayScreen.js';
import Zaika from './components/Zaika.js';
import {Table, TableWrapper, Row, Rows, Col, Cols, Cell } from 'react-native-table-component';
import {
Alert,Platform,
StyleSheet,
Text,
View,TextInput,Keyboard
} from 'react-native';
import NavBar, { NavGroup, NavButton, NavButtonText, NavTitle } from 'react-native-nav';
import { Router, Scene } from 'react-native-router-flux';
import App1 from './App1.js';
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' +
'Cmd+D or shake for dev menu',
android: 'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
export default class App extends Component<{}> {
render() {
return (
<Router>
<Scene key="root">
<Scene
key="zaika"
component={Zaika}
title="Delhi zaika"
initial
/>
<Scene key="app1"
component={App1}
title="app1"
/>
<Scene key="scarlet"
component={ScarletScreen}
title="Scarlet"
/>
<Scene {...this.props}
key="gray"
component={GrayScreen}
title="Gray"
/>
</Scene>
</Router>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
head: { height: 40, backgroundColor: '#f1f8ff' },
text: { marginLeft: 5 },
row: { height: 30 }
});

You won't have instance property props with React Stateless Functional Component.
The props will pass in as first argument instead:
const GrayScreen = (props) => {
return (
<View style={styles.container}>
<Text style={styles.welcome} onPress={() => Actions.black()}>{props.text}</Text>
</View>
)
}

Related

The action 'NAVIGATE' with payload {"name":"Man"} was not handled by any navigator. Do you have a screen named 'Man'? when i dont set a stack.screen

your texti'm tryng to navigate with the props navigation as a nested navigation and i'm getting this error
ERROR The action 'NAVIGATE' with payload {"name":"Man"} was not handled by any navigator.
Do you have a screen named 'Man'?
If you're trying to navigate to a screen in a nested navigator, see https://reactnavigation.org/docs/nesting-navigators#navigating-to-a-screen-in-a-nested-navigator.
This is a development-only warning and won't be shown in production.
i suspact that is becose i didnt set the screen but i dont wont to set the screen becose i wont it to render only when the user is clicking on the button
i'm tryng to navigate the component man
this is my code
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View } from 'react-native';
export default function Man() {
return (
<View style={styles.container}>
<Text>Man</Text>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
import { StatusBar } from "expo-status-bar";
import { StyleSheet, Text, View } from "react-native";
import { NavigationContainer } from "#react-navigation/native";
import { createBottomTabNavigator } from "#react-navigation/bottom-tabs"
import { useNavigation } from '#react-navigation/native';
import Home from "./screens/Home";
import Exhibition from "./screens/Exhibition";
import Cart from "./screens/Cart";
export default function App() {
const Tab = createBottomTabNavigator();
// const navigation = useNavigation();
return (
<NavigationContainer>
<Tab.Navigator >
<Tab.Screen name="Home" component={Home} />
<Tab.Screen name="Exhibition" component={Exhibition} />
<Tab.Screen name="Cart" component={Cart} />
</Tab.Navigator>
</NavigationContainer>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
// marginLeft: 50
},
});
import { StatusBar } from "expo-status-bar";
import {createStackNavigator} from '#react-navigation/stack';
import {
FlatList,
Image,
StyleSheet,
Text,
TouchableOpacity,
View,
} from "react-native";
import { createNativeStackNavigator } from "#react-navigation/native-stack";
import { useState } from "react";
import { useNavigation, StackActions, useNavigationContainerRef } from "#react-navigation/native";
import Man from "./Man";
import Woman from './Woman'
export default function Home({navigation}: any) {
const navigationRef = useNavigationContainerRef()
const collections: { name: string; img: any; compo: any; key: number }[]= [
{
name: "man collection",
img: require("../assets/manModel.jpeg"),
compo: "Man",
key: 1,
},
{
name: "woman collection",
img: require("../assets/womanModel.jpg"),
compo: "Woman",
key: 2,
},
]
const Stack = createNativeStackNavigator();
// const navigation = useNavigation();
return (
<View style={styles.container}>
<FlatList
numColumns={2}
data={collections}
renderItem={({ item }) => (
<>
<TouchableOpacity
onPress={() => {
navigation.navigate('Man');
}}
>
<Image style={styles.image} source={item.img} />
<Text style={styles.title}>{item.name}</Text>
</TouchableOpacity>
</>
)}
/>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
image: {
width: 170,
height: 180,
margin: 15,
},
title: {
position: "absolute",
left: "15%",
right: "15%",
width: "70%",
top: 90,
fontSize: 17,
color: "white",
backgroundColor: "purple",
fontWeight: "bold",
textAlign: "center",
},
});

touchableopacity onPress problem in react navigation.navigate

I tried to implement this codes, But failed. 'undefined is not an object (evaluating 'navigation.navigate') ' error shows when I click that object. I want use TouchableOpacity, not button. How can I fix? or Can I use button without default style in touchableOpacity located place?
import React from 'react'
import {
View,
Text,
StyleSheet,
TouchableOpacity,
Platform,
} from 'react-native'
import { Ionicons } from '#expo/vector-icons'
const ArticleItem = ({
article: {
id,
title,
content,
date,
},
navigation,
}) => {
return (
<View>
<TouchableOpacity
activeOpacity={0.8}
onPress={() => navigation.navigate('ViewStack')} <-error point
>
<View style={styles.container}>
<View style={styles.icon}>
<Ionicons name="md-list" size={14} color="#9E9E9E"/>
</View>
<View style={styles.info}>
<Text style={styles.title}>
{title}
</Text>
<Text style={styles.content}>
{content}
</Text>
<Text style={styles.date}>
{date}
</Text>
</View>
</View>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
padding: 16,
paddingBottom: 0,
},
icon: {
width: 16,
height: 16,
marginRight: 8,
justifyContent: 'center',
alignItems: 'center',
paddingTop: Platform.select({
ios: 3, android: 8,
})
},
title: {
marginBottom: 8,
fontSize: 16,
fontWeight: '800',
color: '#212121',
},
content: {
marginBottom: 4,
fontSize: 14,
color: '#9E9E9E',
lineHeight: 18,
},
date: {
fontSize: 12,
color: '#BDBDBD',
},
info: {
flex: 1,
paddingBottom: 16,
borderBottomWidth: 1,
borderBottomColor: '#DEDEDE',
},
})
export default ArticleItem
ArticleItem.js
import React from 'react'
import {
View,
Text,
} from 'react-native'
import { createStackNavigator } from '#react-navigation/stack'
import ViewDiaryScreen from './ViewDiaryScreen'
const Stack = createStackNavigator();
const ViewStackScreen = () => {
return (
<Stack.Navigator>
<Stack.Screen name="ViewStack" component={ViewDiaryScreen}/>
</Stack.Navigator>
)
}
export default ViewStackScreen
ViewStackScreen.js (placed in screens/ViewStackScreen.js)
import React from 'react'
import {
Text,
StyleSheet,
SafeAreaView,
} from 'react-native'
const ViewDiaryScreen = () => {
return (
<SafeAreaView style={styles.container}>
<Text>
Screen
</Text>
</SafeAreaView>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
}
})
export default ViewDiaryScreen
ViewDiaryScreen.js
import React from 'react'
import {
View,
Text,
} from 'react-native'
import { Ionicons } from '#expo/vector-icons'
import Header from '../components/Header'
import ArticleItem from '../components/ArticleItem'
const DiaryScreen = () => {
return (
<View style={{ flex: 1}}>
<Header title="new Diary" />
<ArticleItem
article={{
id: 1,
title: 'Test1',
content: 'contentTest1',
date: '2021/1/2',
}}
/>
<ArticleItem
article={{
id: 1,
title: 'Test2',
content: 'contentTest2',
date: '2021/1/2',
}}
/>
</View>
);
}
export default DiaryScreen
DiaryScreen.js
As Hend EL-Sahli mentioned, it has nothing to do with your TouchableOpacity but I believe you should have both of the screens in your stack Navigator, Like this:
return (
<Stack.Navigator>
<Stack.Screen name="ArticleItem" component={ArticleItem}/>
<Stack.Screen name="Viewstack" component={ViewDiaryScreen}/>
</Stack.Navigator>
)

How to make components in react native?

I am new to react native and want to create three Components. for Increment Button, Decrement Button and to display Counter value.
State/Hook should remain in the App Component. I don't want to Modify that.
import React, {useState} from 'react';
import { Text, View, StyleSheet, Button } from 'react-native';
export default function App() {
const [getIncCounter, setIncCounter] = useState(0)
const inc = () => {
setIncCounter(getIncCounter + 1);
}
const dec = () => {
setIncCounter(getIncCounter - 1);
}
return (
<View style={styles.container}>
<View style={{flexDirection: 'row'}}>
<Button title="+" onPress={inc} />
<Text> {getIncCounter} </Text>
<Button title="-" onPress={dec} />
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
backgroundColor: '#ecf0f1',
padding: 8,
},
paragraph: {
margin: 24,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
},
});
Here is how you can create a separate component for increase/decrease buttons and a component for showing results.
import React, { useState } from 'react';
import { Text, View, StyleSheet, Button } from 'react-native';
export default function App() {
const [getIncCounter, setIncCounter] = useState(0);
const inc = () => {
setIncCounter(getIncCounter + 1);
};
const dec = () => {
setIncCounter(getIncCounter - 1);
};
return (
<View style={styles.container}>
<View style={{ flexDirection: 'row' }}>
<ButtonComponent title={'+'} onClick={inc} />
<ResultComponent result={getIncCounter} />
<ButtonComponent title={'-'} onClick={dec} />
</View>
</View>
);
}
const ButtonComponent = ({ onClick, title }) => {
return (
<div>
<Button title={title} onPress={onClick} />
</div>
);
};
const ResultComponent = ({ result }) => {
return (
<div>
<Text> {result} </Text>
</div>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
backgroundColor: '#ecf0f1',
padding: 8,
},
paragraph: {
margin: 24,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
},
});
It's redundant though, but if you want to learn how to pass props and stuff it is fine. Button and Text are components themselves.
Expo Snack Example

Trouble with Stack Navigator

I am new to react native. I tried using the createStackNavigator module. However, I do not know why my onClick function is not directing me to the required screen. Here are my codes are shown below:
mySpaceRouter.js
import {createStackNavigator} from 'react-navigation'
import SubscriptionScreen from './subscribed'
import MySpaceScreenRT from './myspace'
import React, {Component} from 'react'
const RootStack = createStackNavigator(
{
MySpace : MySpaceScreenRT,
subscribed : SubscriptionScreen,
navigationOptions:{
header:{ visible:false }
}
},
{
initialRouteName : 'MySpace',
},
)
class MySpaceScreen extends Component{
render(){
return(
<RootStack />
)
}
}
export default MySpaceScreen;
mySpace.js
import React, { Component } from 'react'
import { StyleSheet, Text, View, ScrollView, TouchableOpacity } from 'react-native'
import { Avatar, Button, Icon } from 'react-native-elements'
import MyButton from '../Button'
class MySpaceScreenRT extends Component {
render() {
return (
<View style={styles.container}>
<View style={styles.header}>
<View style={styles.textHolder}>
<Text style={styles.headerText}>My Space</Text>
</View>
</View>
<View style={styles.boxContainer} >
<ScrollView style={styles.scrollContainer}>
<View style={styles.profileContainer}>
<Avatar
large
rounded
title="CR"
onClick={() =>this.props.navigation.navigate('subscribed')}
activeOpacity={0.7}
/>
<Text style={styles.profileName}>Christaino Ronaldo </Text>
</View>
<MyButton text='Subscribed' icon='ios-play' />
<MyButton text='Downloads' icon='ios-folder-open' onPress ={() => console.log('Works!')} />
<MyButton text='History' icon='ios-timer-outline' />
<MyButton text='Rate Our App' icon='ios-star-half' />
</ScrollView>
</View>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flexDirection: 'column',
flex: 1,
},
header: {
height: 70,
backgroundColor: '#780c1c',
elevation: 5,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center'
},
boxContainer: {
flex: 1,
flexDirection: 'column',
},
textHolder: {
},
headerText: {
fontSize: 20,
color: 'white'
},
profileContainer: {
height: 150,
borderColor : '#696969',
borderBottomWidth: 0.5,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'transparent',
},
profileName: {
position: 'relative',
zIndex: 1,
fontSize: 16,
color: '#000000',
alignSelf: 'center',
marginTop: 10,
marginLeft: 10
},
scrollContainer: {
flexDirection: 'column',
},
icons: {
marginTop: 10
},
Text: {
fontSize: 18,
alignSelf: 'center',
padding: 10
}
})
export default MySpaceScreenRT;
subscribed.js
import React, {Component} from 'react'
import {StyleSheet, Text, View} from 'react-native'
class SubscriptionScreen extends Component {
render(){
return(
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>SubscriptionScreen!</Text>
</View>
)
}
}
export default SubscriptionScreen;
Thank you.
With react-native you use onPress() not onClick().
onPress={() =>this.props.navigation.navigate('subscribed')}
For each screen in the stack you have to create an entry so you should do something like this:
const RootStack = createStackNavigator({
MySpace: {
screen: MySpace,
navigationOptions: ({ navigation }) => ({
title: "My Space" ,
header:{ visible:false }
}),
},
subscribed: {
screen: SubscriptionScreen,
navigationOptions: ({ navigation }) => ({
title: "" ,
header:{ visible:false }
}),
}
},{
initialRouteName : 'MySpace',
})
in addition to that you have to change onClick to onPress

How to change state value in react native?

I have problem in changing value of state.It gives mi following error
"Undefine is not a function(evaluating '
this.setState({
switchValue:false
})')"
I have search on stack overflow but the solution is not work for me.
import React, { Component } from 'react';
import {
Alert,
AppRegistry,
StyleSheet,
Text,
View,
Switch
} from 'react-native';
export default class AwesomeProject extends Component {
constructor(props) {
super(props);
this.state = {switchValue: true};
// Toggle the state every second
}
_onPressButton() {
this.setState({
switchValue: false
});
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Switch
onValueChange={this._onPressButton}
value={this.state.switchValue }
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject);
The this inside _onPressButton is not referencing the correct reference. On that case, this inside that _onPressButton is actually reference the button. You can either do this two ways.
<Switch
onValueChange={this._onPressButton.bind(this)}
value={this.state.switchValue }
/>
Or
<Switch
onValueChange={() => this._onPressButton()}
value={this.state.switchValue }
/>