在屏幕之间导航以按标题图像

时间:2020-07-03 09:43:40

标签: react-native react-navigation react-navigation-stack stack-navigator react-android

一个简单的问题:按下sudo rm /var/lib/mongodb/mongod.lock sudo mongod --dbpath /var/lib/mongodb/ --repair sudo mongod --dbpath /var/lib/mongodb/ --journal 标头的图像后,我试图从HomeScreen.js导航到MessageScreen

但是,当我按下图像时,会弹出一个错误:

HomeScreen.js

导航有什么问题? 这是TypeError: navigation.navigate is not a function (In 'navigation-navigate("MessageScreen")', 'navigation.navigate' is undefined)

中的主要代码
App.js

3 个答案:

答案 0 :(得分:1)

您应该将props传递给您的函数,否则它无法知道此“导航”是什么,请尝试类似的

headerRight: ({navigation})=>  
                <TouchableOpacity 
                    onPress={() => navigation.navigate("MessageScreen")}>
                        <Image 
                        source={require("./assets/message.png")} 
                        style={{width: 40,height: 40, borderRadius: 40 / 2}}/>
                </TouchableOpacity>

还是这个?

headerRight: (props)=>  
                <TouchableOpacity 
                    onPress={() => props.navigation.navigate("MessageScreen")}>
                        <Image 
                        source={require("./assets/message.png")} 
                        style={{width: 40,height: 40, borderRadius: 40 / 2}}/>
                </TouchableOpacity>

答案 1 :(得分:1)

尝试一下:

const HomeStack = createStackNavigator({
      HomeScreen: {
        screen: HomeScreen,
        navigationOptions: ({navigation}) => {
          return {
            headerShown: true,
            title:'As minhas viagens',
            headerTintColor:'black',
            headerRight: () =>  
                <TouchableOpacity onPress={() => navigation.navigate("MessageScreen")}>
                    <Image 
                    source={require("./assets/message.png")} 
                    style={{width: 40,height: 40, borderRadius: 40 / 2}}/>
                </TouchableOpacity>
          }
        })
      },
      MessageScreen: {
        screen: MessageScreen,
        navigationOptions: () => ({
          headerShown: true
        })
      }
    });

答案 2 :(得分:0)

您是否尝试过使用:

this.props.navigation.navigate("MessageScreen")

props.navigation.navigate("MessageScreen")吗?