【问题标题】:Navigate to a screen of parent navigator from child navigator screen从子导航器屏幕导航到父导航器屏幕
【发布时间】:2019-11-12 09:10:18
【问题描述】:

我是 React Native 的新手,我一直在构建一个简单的 Food Recipe 应用程序。我的主要Stack Navigator 包含两个屏幕:Bottom Tab NavigatorRecipe screen。我在Home screenTab Navigator 中,当我按下任何配方时,我想传递配方道具并导航到主要Stack Navigator 中的Recipe screen。不幸的是,我还没有找到任何解决方案。 你能帮我解决这个问题吗?

这是主文件App.js

const Navigator = createBottomTabNavigator(
  {
    Home: Home,
    Add: Add,
    Profile: Profile
  }
);

const Stack = createStackNavigator(
  {
    Navigator: Navigator,
    Recipe: Recipe
  },
  {
    headerMode: "none",
    navigationOptions: {
        headerVisible: false,
    }
  }
);

export default createAppContainer(Stack)

这是我的Home Screen 课程

export default class Home extends Component {
    render() {
        return (
            <SafeAreaView style={styles.container}>
                <View style={styles.header}>
                    <Text style={styles.headerTitle}>Recipes</Text>
                </View>

                <ScrollView style={styles.categoryContainer}>
                    <Category name={"All Foods"} recipes={recipes} />
                    <Category name={"Meat"} recipes={[ recipes[1], recipes[3] ]} />
                    <Category name={"Vegetarian"} recipes={[ recipes[0], recipes[2] ]} />
                    <Category name={"Desserts"} recipes={[ recipes[4] ]} />
                </ScrollView>
            </SafeAreaView>
        )
    }
}

这是Category

export default class Category extends Component {
    render() {
        return (
            <View style={styles.container}>
                <View style={styles.categoryHeader}>
                    <Text style={styles.categoryHeaderTitle}>{this.props.name}</Text>
                </View>

                <ScrollView >
                    <View style={styles.categoryContent}>
                        <FlatList
                            data={this.props.recipes}
                            renderItem={({ item }) => <Block name={item.name} image={item.image} time={item.time} portions={item.portions} />}
                            keyExtractor={(item, index) => index.toString()}
                            horizontal={true}
                        /> 
                    </View>  
                </ScrollView>
            </View>
        )
    }
}

这是我想要一直导航回Recipe screen 并使用onPress={} 函数传递配方道具的实际配方块

export default class Block extends Component {
    render() {
        return (
            <TouchableOpacity style={styles.container} onPress={null} >
                <Image style={styles.image} source={this.props.image} />

                <Text style={styles.title}>{this.props.name}</Text>
                <Text style={styles.info}>{this.props.time} min | portions: {this.props.portions}</Text>
            </TouchableOpacity>
        )
    }
}

谢谢大家的回答

【问题讨论】:

    标签: react-native react-navigation


    【解决方案1】:

    如果您需要StackNavigatorBottomTabNavigator 顶部显示标题,我建议为bottomTabBar 的每个子项使用StackNavigator。这样做,您可以在单个 tabBarItem 中拥有更多屏幕,并且您不会让其他屏幕的底部标签栏消失。

    如果您不介意这一点,您可以使用dangerouslyGetParent 从他的孩子那里访问您的stackNavigator,然后使用您需要的道具进行导航。

    navigateToRecipe = (recipe) => {
       this.props.navigation.dangerouslyGetParent().navigate("Recipe", { recipe : myRecipe}) //pass an object with the data you need
    }
    

    然后您可以使用 getParam this.props.navigation.getParam("recipe") 或直接使用 this.props.navigation.state.params 访问您的食谱道具

    【讨论】:

    • 谢谢,这很好用。但是,出现了一个新问题,那就是我没有将this.props.navigation 正确地跨类传递到Block.js 类,因此输出了TypeError: undefined is not an object (evaluating –this.props.navigation.dangerouslyGetParent) 错误。如何通过这些正确的方式使其工作?
    • 您可以将它从父级传递到阻止执行 navigation = { this.props.navigation } 或使用 react-navigation 提供的 withNavigation HOC :reactnavigation.org/docs/en/with-navigation.html
    • 使用navigation = { this.props.navigation } 一直传递给Block 类后,React Native 仍然无法识别该道具。但是,第二个选项工作得很好。非常感谢您的帮助!
    猜你喜欢
    • 2017-12-12
    • 1970-01-01
    • 2020-03-06
    • 2022-10-07
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    相关资源
    最近更新 更多