简体   繁体   English

使用 onPress 将值从父级传递给子级

[英]Passing a value using onPress from parent to child

I am working on an app and I want to pass in a key from a dictionary to a child screen.我正在开发一个应用程序,我想将一个键从字典传递到子屏幕。 I have the dictionary in the parent, the code below shows what I want to accomplish我在父母中有字典,下面的代码显示了我想要完成的事情

import ImageDetail from "../components/ImageDetail";

var Salad = {
    GreekSalad: ["Greek Salad", "Romaine lettuce, cucumber, onion, olives, tomatoes, bell peppers, feta cheese dressed with lemon, salt, and olive oil dressing",require('../../assets/greeksalad.jpg'), 10],
    ArabicSalad: ["Arabic Salad", "Diced cucumbers, tomato, green onion, parsley dressed with lemon, salt, and olive oil dressing",require('../../assets/arabicsalad.jpg'), 8],
    Tabula: ["Tabouli", "fine chopped parsley, cracked wheat, tomatoes, and cucumber dressed with lemon salt and olive oil dressing", 8],
    KHSS: ["KH Special Salad", "Romaine lettuce, cucumbers, onions, tomatoes, onions, apples, mint, bell peppers, sunflower seeds, and zaatar dressed with lemon, salt, and olive oil dressing", 12],
    LSCup: ["Lentil Soup", "Flavorful lentil, carrot, and onion pureed soup(Cup 8oz)", 5],
    LSBowl: ["Lentil Soup", "Flavorful lentil, carrot, and onion pureed soup(Bowl 12oz)", 7]

};

The next portion of the code is the part that Is difficult for me to finish, specifically the touchable opacity passing the Salads.greeksalad key and value into the next page代码的下一部分是我很难完成的部分,特别是将 Salads.greeksalad 键和值传递到下一页的可触摸不透明度

const Salads = () => {
    return (<View>
            <ScrollView>
                <View style={styles.backGround}>
                    <View style={styles.container}>
                        <Image style={styles.logostyle} source={require('../../assets/KabobHouseLogo.jpg')}/>
                    </View>
                    <Text style={styles.title}>Salads{"\n"}</Text>    
                    <View style={styles.container1}>
                    <TouchableOpacity onPress={(Salad.GreekSalad) => {props.navigation.navigate("InnerMenu")}}>
                        <ImageDetail title={Salad.GreekSalad[0]} description={Salad.GreekSalad[1]} imageSource={Salad.GreekSalad[2]} price={Salad.GreekSalad[3]}/>
                    </TouchableOpacity>
                        <ImageDetail title={Salad.ArabicSalad[0]} description={Salad.ArabicSalad[1]} imageSource={Salad.ArabicSalad[2]} price={Salad.ArabicSalad[3]}/>
                        <ImageDetail title={Salad.Tabula[0]}  description={Salad.Tabula[1]}  imageSource={require('../../assets/KabobHouseLogo.jpg')}      price={Salad.Tabula[2]}/>
                        <ImageDetail title={Salad.KHSS[0]}  description={Salad.KHSS[1]}  imageSource={require('../../assets/KabobHouseLogo.jpg')} price={Salad.KHSS[2]}/>

This is how you pass values in React Navigation to the next screen这就是你将 React Navigation 中的值传递到下一个屏幕的方式

// navigation.navigate('RouteName', { /* params go here */ })
const nextScreen = ()=>{
    props.navigation.navigate("InnerMenu",{saladKey:Salad.GreekSalad[1]})
}

Then pass the function into onPress然后将 function 传入onPress

<TouchableOpacity onPress={nextScreen}>
   <ImageDetail title={Salad.GreekSalad[0]} description={Salad.GreekSalad[1]} imageSource={Salad.GreekSalad[2]} price={Salad.GreekSalad[3]}/>
</TouchableOpacity>

This is how you access them in the next screen这是您在下一个屏幕中访问它们的方式

const childScreen = (props)=>{
  const saladKey = props.route.params.saladKey;
}

Lastly I recommended that you don't make a habit of passing around values between screens.最后,我建议您不要养成在屏幕之间传递值的习惯。 Instead for most cases you can use a thing called useContext which lets you share state between all your screens.相反,在大多数情况下,您可以使用一个名为useContext的东西,它可以让您在所有屏幕之间共享 state。 Read more here ( https://dmitripavlutin.com/react-context-and-usecontext/ )在此处阅读更多信息( https://dmitripavlutin.com/react-context-and-usecontext/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM