navigate to another screen using react native - react-native

I am trying to navigate to another screen while pressing on a button using onpress in react native but getting error. Front page is App.js which contains multiple buttons, I am working on event button for now. when I press on event button then it should redirect to the contain in event.js I am a newbie in react.
App.js
import React from "react";
import {StyleSheet,Text,View,Button,Image,ProgressViewIOS} from "react-native";
import event from './event.js';
export default class App extends React.Component {
render() {
return (
<View>
<Text style={styles.h1}>WELCOME TO </Text>
<Image
style={styles.logo}
source={require('./logo_tiny.jpg')}
/>
<View style={styles.container}>
<View style={styles.b1}>
<Button
title="Events"
onPress={() => this.onPress('navigateToevent')}
color="#fff"
/>
</View>
<View style={styles.b1}>
<Button
title="Package"
onPress={() => this.onPress()}
color="#fff"
/>
</View>
</View>
<View style={styles.container}>
<View style={styles.b1}>
<Button
title="Promotion"
onPress={() => this.onPress()}
color="#fff"
/>
</View>
<View style={styles.b1}>
<Button
title="Support"
onPress={() => this.onPress()}
color="#fff"
/>
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
h1: {
textAlign: 'center',
color: '#1E7778',
fontSize: 32,
marginTop: 18,
width: 400,
fontWeight: 'bold',
},
container: {
flexDirection: 'row',
},
b1: {
width: '40%',
margin: 20,
padding: 10,
backgroundColor: '#D16B11',
borderRadius: 20,
},
logo: {
alignItems: 'center',
justifyContent:'center',
left: '20%',
},
});
event.js
import React from 'react';
export default class event extends React.Component {
navigateToevent = () => {
this.props.navigation.navigate('event');
};
constructor(props) {
super(props);
this.state = {
country: null,
city: null,
cities: []
};
}
changeCountry(item) {
let city = null;
let cities;
switch (item.value) {
case 'fr':
cities = [
{label: 'Paris', value: 'paris'}
];
break;
case 'es':
cities = [
{label: 'Madrid', value: 'madrid'}
];
break;
}
this.setState({
city,
cities
});
}
changeCity(item) {
this.setState({
city: item.value
});
}
render() {
return (
<>
<DropDownPicker
items={[
{label: 'France', value: 'fr'},
{label: 'Spain', value: 'es'},
]}
defaultNull
placeholder="Select your country"
containerStyle={{height: 40}}
onChangeItem={item => this.changeCountry(item)}
/>
<DropDownPicker
items={this.state.cities}
defaultNull={this.state.city === null}
placeholder="Select your city"
containerStyle={{height: 40}}
onChangeItem={item => this.changeCity(item)}
/>
</>
);
}
}

// event.js
import React from "react";
import DropDownPicker from "react-native-dropdown-picker";
export default class event extends React.Component {
constructor(props) {
super(props);
this.state = {
country: null,
city: null,
cities: [],
};
}
changeCountry(item) {
let city = null;
let cities;
switch (item.value) {
case "fr":
cities = [{ label: "Paris", value: "paris" }];
break;
case "es":
cities = [{ label: "Madrid", value: "madrid" }];
break;
}
this.setState({
city,
cities,
});
}
changeCity(item) {
this.setState({
city: item.value,
});
}
render() {
return (
<>
<DropDownPicker
items={[
{ label: "France", value: "fr" },
{ label: "Spain", value: "es" },
]}
defaultNull
placeholder="Select your country"
containerStyle={{ height: 40 }}
onChangeItem={(item) => this.changeCountry(item)}
/>
<DropDownPicker
items={this.state.cities}
defaultNull={this.state.city === null}
placeholder="Select your city"
containerStyle={{ height: 40 }}
onChangeItem={(item) => this.changeCity(item)}
/>
</>
);
}
}
// main.js
import React from "react";
import {
StyleSheet,
Text,
View,
Button,
Image,
ProgressViewIOS,
} from "react-native";
import event from "./event.js";
export default class Main extends React.Component {
navigateToevent = () => {
this.props.navigation.navigate("event");
};
render() {
return (
<View>
<Text style={styles.h1}>WELCOME TO </Text>
<Image style={styles.logo} />
<View style={styles.container}>
<View style={styles.b1}>
<Button
title="Events"
onPress={() => this.navigateToevent()}
color="#fff"
/>
</View>
<View style={styles.b1}>
<Button
title="Package"
onPress={() => this.onPress()}
color="#fff"
/>
</View>
</View>
<View style={styles.container}>
<View style={styles.b1}>
<Button
title="Promotion"
onPress={() => this.onPress()}
color="#fff"
/>
</View>
<View style={styles.b1}>
<Button
title="Support"
onPress={() => this.onPress()}
color="#fff"
/>
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
h1: {
textAlign: "center",
color: "#1E7778",
fontSize: 32,
marginTop: 18,
width: 400,
fontWeight: "bold",
},
container: {
flexDirection: "row",
},
b1: {
width: "40%",
margin: 20,
padding: 10,
backgroundColor: "#D16B11",
borderRadius: 20,
},
logo: {
alignItems: "center",
justifyContent: "center",
left: "20%",
},
});
// App.js
import React from "react";
import {
StyleSheet,
Text,
View,
Button,
Image,
ProgressViewIOS,
} from "react-native";
import event from "./event.js";
import main from "./main.js";
import { createAppContainer } from "react-navigation";
import { createStackNavigator } from "react-navigation-stack";
const AppNavigator = createStackNavigator({
main: {
screen: main,
},
event: {
screen: event,
},
});
class App extends React.Component {
render() {
return (
<View>
<AppNavigator />
</View>
);
}
}
export default AppContainer = createAppContainer(AppNavigator);
Try this works for you

This contains most of what you need with navigation on react native. https://reactnative.dev/docs/navigation
Need to wrap your code in <NavigationContainer>

Related

how to pass data between components using props in react native

I'm new in react-native and I want to pass data from the Home component to the Product component and I import the Product component in the Home component and I map the Product component but I get an error and it says undefined is not an object
Home.js
import * as React from 'react';
import { View, Text, StyleSheet, ScrollView } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { Card, Button } from 'react-native-elements';
import Product from './components/Product';
const BASE_URL = 'https://raw.githubusercontent.com/sdras/sample-vue-shop/master/dist';
const products = [
{
name: 'Khaki Suede Polish Work Boots',
price: 149.99,
img: `${BASE_URL}/shoe1.png`
},
{
name: 'Camo Fang Backpack Jungle',
price: 39.99,
img: `${BASE_URL}/jacket1.png`
},
{
name: 'Parka and Quilted Liner Jacket',
price: 49.99,
img: `${BASE_URL}/jacket2.png`
},
{
name: 'Cotton Black Cap',
price: 12.99,
img: `${BASE_URL}/hat1.png`
},
];
function HomeScreen({ navigation }) {
return (
<ScrollView
style={{
flexGrow: 0,
width: "100%",
height: "100%",
}}>
{
products.map((product, index) => {
return(
<View style={styles.row} key={index}>
<View style={styles.col}>
<Product/>
</View>
</View>
)
})
}
</ScrollView>
);
}
const styles = StyleSheet.create({
row: {
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
},
col: {
flex: 1,
},
});
export default HomeScreen;
Product.js:
import * as React from 'react';
import { View, Text, StyleSheet } from 'react-native';
import { Card, Button } from 'react-native-elements';
import { useNavigation } from '#react-navigation/native';
function Product(props){
const navigation = useNavigation();
return(
<Card
image={{uri: this.props.product.img}}>
<Text style={{marginBottom: 10, marginTop: 20 }} h2>
{this.props.product.name}
</Text>
<Text style={styles.price} h4>
{this.props.product.price}
</Text>
<Text h6 style={styles.description}>
added 2h ago
</Text>
<Button
type="clear"
title='Buy now'
onPress={() => navigation.navigate('Details')}
/>
</Card>
);
}
const styles = StyleSheet.create({
name: {
color: '#5a647d',
fontWeight: 'bold',
fontSize: 30
},
price: {
fontWeight: 'bold',
marginBottom: 10
},
description: {
fontSize: 10,
color: '#c1c4cd'
}
});
export default Product;
Error screenshot:
how to solve my problem
Please update your home component to this
function HomeScreen({ navigation }) {
return (
<ScrollView
style={{
flexGrow: 0,
width: "100%",
height: "100%",
}}>
{
products.map((product, index) => {
return(
<View style={styles.row} key={index}>
<View style={styles.col}>
<Product product={product}/>
</View>
</View>
)
})
}
</ScrollView>
);
}
Basically you need to pass the product as a prop to Product component.
There is no this in functional component. Please update your product component
function Product(props){
const navigation = useNavigation();
return(
<Card
image={{uri: this.props.product.img}}>
<Text style={{marginBottom: 10, marginTop: 20 }} h2>
{props.product.name}
</Text>
<Text style={styles.price} h4>
{props.product.price}
</Text>
<Text h6 style={styles.description}>
added 2h ago
</Text>
<Button
type="clear"
title='Buy now'
onPress={() => navigation.navigate('Details')}
/>
</Card>
);
}
Pass props in home component to product like this.
const products = [
{
name: 'Khaki Suede Polish Work Boots',
price: 149.99,
img: `${BASE_URL}/shoe1.png`,
},
{
name: 'Camo Fang Backpack Jungle',
price: 39.99,
img: `${BASE_URL}/jacket1.png`,
},
{
name: 'Parka and Quilted Liner Jacket',
price: 49.99,
img: `${BASE_URL}/jacket2.png`,
},
{
name: 'Cotton Black Cap',
price: 12.99,
img: `${BASE_URL}/hat1.png`,
},
];
function HomeScreen({navigation}) {
return (
<ScrollView
style={{
flexGrow: 0,
width: '100%',
height: '100%',
}}>
{products.map((product, index) => {
return (
<View style={styles.row} key={index}>
<View style={styles.col}>
<Product product={products} />
</View>
</View>
);
})}
</ScrollView>
);
}
------------Product.js-----------
function Product({product}) {
const navigation = useNavigation();
return (
<Card image={{uri: product.img}}>
<Text style={{marginBottom: 10, marginTop: 20}} h2>
{product.name}
</Text>
<Text style={styles.price} h4>
{product.price}
</Text>
<Text h6 style={styles.description}>
added 2h ago
</Text>
<Button
type="clear"
title="Buy now"
onPress={() => navigation.navigate('Details')}
/>
</Card>
);
}

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',
},
});

React component not re-rendering on state change using setState

I have a HomeScreen component which has button. I am trying to popup a modelview(Seperate component ie PopUpView) on the button click. In PopUpView i am sending its visibility as prop (isVisible).On the click of the button i am trying to change the state value popUpIsVisible from false to true. Hoping that this would re-render and popup my model view(Note this is working fine if i explicitly pass true). However with the state change it look like the render function is not being called and the popUp is not being displayed. Thanks for your help in advance
import React from 'react';
import { View, StyleSheet, Text, Button, TouchableHighlight, Alert, Dimensions} from 'react-native';
import { createStackNavigator, createAppContainer } from 'react-navigation';
import PopUpView from './src/PopUpView';
class HomeScreen extends React.Component {
constructor(props) {
super(props);
this.state = {
popUpIsVisible: false,
};
}
setPopUpIsVisible(isVisible){
this.setState({popUpIsVisible: isVisible });
}
render() {
this.setPopUpIsVisible = this.setPopUpIsVisible.bind(this);
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center', backgroundColor:"blue" }}>
<Text>Home Screen</Text>
<PopUpView isVisible={this.state.popUpIsVisible}/>
<Button onPress={() => {this.setPopUpIsVisible(true)}} title="Open PopUp Screen"/>
</View>
);
}
}
const RootStack = createStackNavigator(
{
Home: HomeScreen
},
{
initialRouteName: 'Home',
}
);
const AppContainer = createAppContainer(RootStack);
export default class App extends React.Component {
render() {
return <AppContainer />;
}
}
PopUpView.js
import React from 'react';
import { View, Modal,StyleSheet, Text, TouchableOpacity, Dimensions} from 'react-native';
import { TabView, TabBar,SceneMap } from 'react-native-tab-view';
import Icon from 'react-native-vector-icons/SimpleLineIcons';
const FirstRoute = () => (
<View style={{ flex: 1, backgroundColor: '#ff4081' }} />
);
const SecondRoute = () => (
<View style={{ flex: 1, backgroundColor: '#673ab7' }} />
);
const ThirdRoute = () => (
<View style={{ flex: 1, backgroundColor: '#673ab7' }} />
);
export default class PopUpView extends React.Component {
constructor(props) {
super(props);
this.state = {
modalVisible:this.props.isVisible,
index: 0,
routes: [
{ key: 'first', title: 'HIGHLIGHTS' },
{ key: 'second', title: 'AMENITIES' },
{ key: 'third', title: 'FACILITIES' },
],
};
}
setModalVisible(visible) {
this.setState({modalVisible: visible});
}
renderHeader = props => <TabBar
{...props}
indicatorStyle={{backgroundColor: 'red'}}
tabStyle={styles.bubble}
labelStyle={styles.noLabel}
/>;
render() {
return (
<Modal
animationType="slide"
transparent={true}
visible={this.state.modalVisible}
onRequestClose={() => {
Alert.alert('Modal has been closed.');
}}>
<View style={styles.container}>
<View style={styles.navBar}>
<Text style={styles.navBarTitle}>Test</Text>
<TouchableOpacity
onPress={() => {
this.setModalVisible(!this.state.modalVisible);
}}>
<Icon style={styles.closeButton} name="close" size={35} color="grey" />
</TouchableOpacity>
</View>
<TabView
navigationState={this.state}
renderScene={SceneMap({
first: FirstRoute,
second: SecondRoute,
third: ThirdRoute,
})}
onIndexChange={index => this.setState({ index })}
initialLayout={{ width: Dimensions.get('window').width }}
renderTabBar={props =>
<TabBar
{...props}
style={{ backgroundColor: 'white' }}
indicatorStyle={{backgroundColor: 'black'}}
tabStyle={styles.bubble}
labelStyle={styles.label}
/>
}
/>
</View>
</Modal>
);
}
}
const styles = StyleSheet.create({
container: {
flex:1,
margin: 50,
marginLeft: 20,
marginRight: 20,
marginBottom: 20,
backgroundColor: "white",
borderWidth: 1,
borderColor: "grey",
flexDirection: 'column'
},
navBar:{
height:70,
justifyContent: 'space-between',
alignItems: 'center',
flexDirection: 'row',
borderBottomColor: 'lightgrey',
borderBottomWidth: 1,
},
navBarTitle:{
fontSize: 25,
fontFamily: 'Optima',
paddingLeft:15,
},
closeButton:{
paddingRight:12,
},
label: {
color: 'black'
}
})
The problem with your code is that you are using the state inside the PopUpView which does not change when you change the external prop. to fix this you should use the componentwillreceiveprops and update your state accordingly.
componentWillReceiveProps(nextProps){
if(this.props.isVisible!=nextProps.isVisible){
this.setState({modalVisible:nextProps.isVisible})
}
}
The better approach will be using the this.props.isVisible as the visible prop for the Model.
In this scenario you will have to pass a function as a prop to popupview which will set the popUpIsVisible to false.
Something like below
<PopUpView isVisible={this.state.popUpIsVisible}
onDismiss={()=>{this.setState({popUpIsVisible:false})}}/>
You can call the onDismiss inside the child as
<Modal visible={this.props.isVisible}>
<TouchableHighlight
onPress={() => {
this.props.onDismiss();
}}>
<Text>Hide Modal</Text>
</TouchableHighlight>
</Modal>
The second approach is better as the visibility of the child is controlled by the parent.
Add below line into constructor after state defining
this.setPopUpIsVisible = this.setPopUpIsVisible.bind(this);

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.

React Native: Navigation not working in component

I can not open the third page screen with the On Press method in the List Component.js file. I do not understand the problem. I want to know what I'm doing wrong with this. Where is the problem? Thank you. Please help.
Thank you for your reply but this will not work, my friend. I've updated the ListComponent.js file. The ListComponent.js file is actually a list. Please can you look again. ?
App.js
import React, { Component } from 'react';
import {
WebView,
AppRegistry, StyleSheet, Text, View, Button
} from 'react-native';
import { StackNavigator } from 'react-navigation';
import ListComponent from './ListComponent';
class App extends Component {
static navigationOptions =
{
title: 'App',
};
OpenSecondActivityFunction = () =>
{
this.props.navigation.navigate('Second');
}
render() {
return (
<View style={styles.container}>
<Button onPress = { this.OpenSecondActivityFunction } title = 'Open Second Activity'/>
</View>
);
}
}
class SecondActivity extends Component
{
static navigationOptions =
{
title: 'SecondActivity',
};
OpenThirdActivityFunction = () =>
{
this.props.navigation.navigate('Third');
}
render()
{
return(
<View style={{ flex: 1}}>
<ListComponents data={alapinCtrl} OpenThirdActivityFunction={this.OpenThirdActivityFunction} />
</View>
);
}
}
class ThirdActivity extends Component
{
static navigationOptions =
{
title: 'ThirdSecondActivity',
};
render()
{
return(
<View style={{ flex: 1}}>
<Text>3</Text>
</View>
);
}
}
export default ActivityProject = StackNavigator(
{
First: { screen: App },
Second: { screen: SecondActivity },
Third: { screen: ThirdActivity }
});
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},ActivityNameTextCss:
{
fontSize: 22,
color: 'black',
textAlign: 'center',
},
});
AppRegistry.registerComponent('ActivityProject', () => ActivityProject);
ListComponent.js
import React, { Component } from "react";
import {AppRegistry,View, Text, FlatList, ActivityIndicator} from "react-native";
import { List, ListItem, SearchBar } from "react-native-elements";
class ListComponents extends Component {
constructor(props) {
super(props);
this.state = {
loading: false,
data: [],
page: 1,
seed: 1,
error: null,
refreshing: false
};
}
renderSeparator = () => {
return (
<View
style={{
height: 1,
width: "98%",
backgroundColor: "#CED0CE",
marginLeft: "2%"
}}
/>
);
};
renderHeader = () => {
return <SearchBar placeholder="Type Here..." lightTheme round />;
};
renderFooter = () => {
if (!this.state.loading) return null;
return (
<View
style={{
paddingVertical: 20,
borderTopWidth: 1,
borderColor: "#CED0CE"
}}
>
<ActivityIndicator animating size="large" />
</View>
);
};
render() {
return (
<List containerStyle={{ borderTopWidth: 0, borderBottomWidth: 0 }}>
<FlatList
data={this.props.data}
renderItem={({ item }) => (
<ListItem
roundAvatar
title={`${item.name}`}
subtitle={item.coders}
containerStyle={{ borderBottomWidth: 0 }}
onPress={() => this.props.OpenThirdActivityFunction(item.coders)}
/>
)}
keyExtractor={item => item.coders}
ItemSeparatorComponent={this.renderSeparator}
ListHeaderComponent={this.renderHeader}
ListFooterComponent={this.renderFooter}
/>
</List>
);
}
}
export default ListComponents;
Only SecondActivity has the navigation prop; you need to explicitly pass it to child component:
<ListComponent navigation={this.props.navigation} />
Alternatively,
<ListComponent onPress={() => this.props.navigation.navigate('Third')} />;
Then just do <Button onPress={this.props.onPress} in your ListComponent.