I want to pass parameters to a view. I followed some examples, but to no avail. How do I do it?
A part of my code:
Home:
import React from 'react';
import { MaterialIcons } from '#expo/vector-icons';
import { useNavigation } from '#react-navigation/native';
import { View, Text, Image, StyleSheet, ScrollView } from 'react-native';
import colors from '../../components/colors';
import Shoe from '../../components/home/shoe';
import Teste from '../teste';
import Detail from '../detail';
export default function Home(props) {
return(
<View style={styles.storeProducts_line}>
<Shoe
source={require('../../images/home/tenisAdidasFalconFeminino.jpg')}
title="Tênis Adidas Falcon Feminino"
price="200,00"
onClick={() => props.navigation.navigate('Detail', {
productId: 12,
productTitle: 'Tênis Adidas',
})}
/>
<Shoe
source={require('../../images/home/tenisAdidasYeezySalt.jpg')}
title="Tênis Adidas Yeezy Salt"
price="175,90"
onClick={() => navigation.navigate('Detail')}
/>
</View>
);
}
Detail:
import React from 'react';
import { View, Text, StyleSheet, ScrollView, Image } from 'react-native';
export default function Detail({props, navigation}) {
const productId = navigation.getParam('userId');
// const productId = props.navigation.getParam('userId');
return(
<View style={styles.container}>
<Image
/>
<Text>
Detail
</Text>
<Text>
{productId}
</Text>
</View>
);
}
All the examples I saw the structure of the view is different, do I have to change the structure to work? I'm new to react-native, bear with me. Thanks in advance for your attention.
You can also use route to access the passed data from parent component.
Example:
import React from 'react';
import { View, Text, StyleSheet, ScrollView, Image } from 'react-native';
export default function Detail({route, navigation}) {
const {productId} = route.params;
// const productId = props.navigation.getParam('userId');
return(
<View style={styles.container}>
<Image
/>
<Text>
Detail
</Text>
<Text>
{productId}
</Text>
</View>
);
}
For more please do read this doc: Passing parameters to routes
Related
I'm not sure what's wrong here, as far as I can tell I am doing this correctly as it has worked everywhere else in my app. The only difference is that I'm trying to do this from a FlatList component. What is wrong with this? I've also tried putting {navigation} in renderMyItem instead of FLToyCard brackets, didn't work either. Error I'm getting is:
TypeError: undefined is not an object (evaluating 'navigation.navigate').
import { StyleSheet, Text, View, FlatList } from 'react-native'
import React from 'react'
import Toy from './Database'
import ToyCard from './ToyCard'
const FLToyCard = ({navigation}) => {
const headerComp = () => {
return(
<View style={{alignSelf: 'center'}}>
<Text style={{fontSize: 25, padding: 10}}>All Toys For Sale</Text>
</View>
)
}
const renderMyItem = ({item}) => {
return(
<View style={{flex: 1}}>
<ToyCard
name={item.name}
image={item.image}
price={item.price}
desc={item.desc}
seller={item.seller}
onPress={()=>navigation.navigate('SlugProduct')}
/>
</View>
)
}
return(
<View>
<FlatList
data={Toy}
renderItem={renderMyItem}
keyExtractor={(item)=>item.id}
numColumns={2}
ListHeaderComponent={headerComp}
/>
</View>
)
}
export default FLToyCard
This is my App.js:
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View } from 'react-native';
import { NavigationContainer } from '#react-navigation/native'
import { createNativeStackNavigator } from '#react-navigation/native-stack';
import SlugProduct from './Screens/SlugProduct';
export default function App() {
const Stack = createNativeStackNavigator()
return (
<NavigationContainer>
<Stack.Navigator initialRouteName='Login' >
// list of other pages
<Stack.Screen name="SlugProduct" component={SlugProduct} />
<Stack.Screen name='ViewToys' component={ViewToys}
</Stack.Navigator>
</NavigationContainer>
);
}
EDIT: heres the ViewToys page that FLToyCard is being input into. I've tried adding the onPress into both Views and FLToyCard but got the same error each time.
import { StyleSheet, Text, View, ScrollView} from 'react-native'
import React from 'react'
import FLToyCard from '../Components/FlatListCards'
const ViewToys = ({navigation}) => {
return (
<View style={{backgroundColor: '#ffce20', height: '100%'}}>
<View>
<FLToyCard />
</View>
</View>
)
}
export default ViewToys
The issue is that your not passing navigation props from screen ViewToys into component FLToyCard.
Try this
In ViewToys Screen:
import { StyleSheet, Text, View, ScrollView} from 'react-native'
import React from 'react'
import FLToyCard from '../Components/FlatListCards'
const ViewToys = ({navigation}) => {
return (
<View style={{backgroundColor: '#ffce20', height: '100%'}}>
<View>
<FLToyCard navigation={navigation}/>
</View>
</View>
)
}
export default ViewToys
change this <FLToyCard /> to this <FLToyCard navigation={navigation} />
and you are all done.
you are getting undefined in navigation in FLToyCard component that's why you are getting this error.
This error come when we do not put these mention in a page
import { NavigationContainer,useFocusEffect } from '#react-navigation/native';
import { createNativeStackNavigator } from '#react-navigation/native-stack';
So I think put above in every page once your problem will solve,
I am using Navigate to go to another page on React Native, I've tried with navigation & react Dom. I want to go to Home.js. I've tried using NavigationContainer, & reateStackNavigator and also React Dom.
I can't seem to find a solution to either of the two and the YouTube videos aren't really helping.
import {
StyleSheet,
Text,
SafeAreaView,
Image,
View,
TouchableOpacity,
} from 'react-native';
import React,{ useState} from 'react';
import {Card} from 'react-native-shadow-cards';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
import {Route, BrowserRouter as Router, Switch, Link, Navigate, NavigationType} from 'react-router-dom';
export default function App() {
const handlePress = () => navigation.navigate('Home');
const [name,setName] = useState("");
const shadowstyle={
shadowOpacity:1
};
return (
<SafeAreaView style={styles.container}>
<Image
source={require("/Users/travisnunnally/Malik Projects VSCODE/Gtipz/assets/Star.png")} />
<Image
source={require("/Users/travisnunnally/Malik Projects VSCODE/Gtipz/assets/Star.png")} />
<Text style={styles.titleText} onPress={handlePress}>GTipz </Text>
<Image
source={require("/Users/travisnunnally/Malik Projects VSCODE/Gtipz/assets/GtipzLogo.png")} />
<View>
<Image
source={require("/Users/travisnunnally/Malik Projects VSCODE/Gtipz/assets/Star.png")} />
</View>
<TouchableOpacity onPress={handlePress}>
<View style ={[styles.buttonContainer, styles.userImageWrapper]}>
<Text style={styles.buttonText}>Login</Text>
</View>
You need to import the useNavigation hook from react-navigation.
import { useNavigation } from '#react-navigation/native'
const navigation = useNavigation()
I also suggest you replace Text with TouchableOpacity where you call the handlePress function
Please refer to this doc for more information.
Home.js
this is the code for my Home.js screen
import React from "react";
import { StyleSheet, Text, View, Image, FlatList, TouchableOpacity, ImageBackground } from "react-native";
import colors from "../assets/Colors/colors";
import { ScrollView } from "react-native";
import { Entypo } from '#expo/vector-icons';
import activitiesData from "../assets/Data/activitiesData";
import discoverCategoriesData from "../assets/Data/discoverCategoriesData";
import discoverData from "../assets/Data/discoverData";
import learnMoredata from "../assets/Data/learnMoredata";
import { SafeAreaView } from "react-native-safe-area-context";
import Profile from "../assets/Images/person.png";
import { MaterialIcons } from '#expo/vector-icons';
import Details from "./Details";
import {
Lato_100Thin,
Lato_100Thin_Italic,
Lato_300Light,
Lato_300Light_Italic,
Lato_400Regular,
Lato_400Regular_Italic,
Lato_700Bold,
Lato_700Bold_Italic,
Lato_900Black,
Lato_900Black_Italic
} from '#expo-google-fonts/lato';
import { useFonts } from "#expo-google-fonts/lato";
import AppLoading from "expo-app-loading";
const Home = ({ navigation }) => {
const renderDiscoverItem = ({item}) => {
return (
<TouchableOpacity
onPress={() =>
navigation.navigate("Details")
}>
<ImageBackground
source={item.image}
style={[
styles.disoverItem,
{ marginLeft: item.id === "discover-1" ? 20 : 0 },
]}
imageStyle={styles.disoverItemImage}
>
<Text style={styles.discoverItemTitle}>{item.title}</Text>
<View style={styles.discoverItemLocationWrapper}>
<MaterialIcons name="location-pin" size={24} color="white" />
<Text style={styles.discoverItemLocationText}>{item.location}</Text>
</View>
</ImageBackground>
</TouchableOpacity>
)
}
Details.js
this is the code for my Details.js screen
import React from "react";
import { Text, View } from "react-native";
const Details = () => {
return (
<View>
<Text>
Details
</Text>
</View>
);
}
export default Details;
this is the code for my home and details screen I need to tap on the box then I should redirect on to the details screen
Look at this image
can you help me resolve this issue fast I need to complete my project
update this in your app.js
<NavigationContainer>
<AppStack.Navigator>
<AppStack.Screen name='Details' component={Details} />
</AppStack.Navigator>
</NavigationContainer>
I am trying to make a back button in react native and using navigation.goBack() in the function component but if I console the navigation then it's giving me undefined
import React from 'react';
import {
Text,
StyleSheet,
Image,
View,
Dimensions,
TouchableOpacity,
} from 'react-native';
import backIcon from '../assets/back-black.png';
const {width: SCREEN_WIDTH, height: SCREEN_HEIGHT} = Dimensions.get('window');
const TitleHeader = ({navigation, title}) => {
// console.log("hello");
return (
<View style={[styles.customHeader]}>
<View style={[styles.headerLeft]}>
<TouchableOpacity onPress={() => navigation.goBack()}>
<Image source={backIcon} style={styles.headerIcon} />
</TouchableOpacity>
<Text style={[styles.font, styles.headerTitle]}>{title}</Text>
</View>
</View>
);
};
The rest of the code is working fine I am just getting an error in navigation so I add the important code only
React-navigation v5 provides hook useNavigation() returns the navigation prop of the screen it's inside.
Sample:
import * as React from 'react';
import { Button } from 'react-native';
import { useNavigation } from '#react-navigation/native';
function MyBackButton() {
const navigation = useNavigation();
return (
<Button
title="Back"
onPress={() => {
navigation.goBack();
}}
/>
);
}
In your code you need pass prop navigation to your component
When I run this I get an error saying: Element type is invalid: expected a string (built in components) or a class/function but got undefined, check the render method for LOGIN.
I just want to display login on main screen from other JS file
This is the LOGIN.JS <--- Where I call in the index
'use strict';
import React, { Component, Image} from 'react';
import {View, Text, StyleSheet} from 'react-native';
var Login = React.createClass({
render: function() {
return (
<View style={styles.container}>
<Image style={styles.logo}
source={require('image!lock')} />
<Text>HI</Text>
</View>
);
}
});
INDEX.IOS.JS <-- Where I am calling Login to show on page
import React, { Component, Image } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';
var Login = require('./Login');
export default class AwesomeProject extends Component {
render() {
return (
<View style={styles.container} >
<Text style={styles.welcome}>
Welcome!
</Text>
<Text style={styles.logins}>
</Text>
<Login />
</View>
);
}
}
React render function should return a View element to render.
Change this
var Login = React.createClass({ render: function() { return ( HI ); } });
to
var Login = React.createClass({
render: function() {
return (
<View style={{flex:1}}>
<Text>HI</Text>
</View>
);
}
});
You can use the same syntax for the Login.js file as the index.ios.js and declare it with ES6 Class syntax, and export it.
Also FYI it appears you are importing Image from React rather than React Native in both files.
import React, { Component} from 'react';
import {View, Text, StyleSheet, Image} from 'react-native';
export default class Login extends Component {
render() {
return (
<View style={styles.container}>
<Image style={styles.logo}
source={require('image!lock')} />
<Text>HI</Text>
</View>
);
}
}