简体   繁体   English

单击按钮时对本机导航做出反应

[英]react native navigation on button click

i have this code and i need to navigate to another component on button press我有这个代码,我需要在按下按钮时导航到另一个组件

    const App = () => {
....}
export default App;

---this is the part where im trying to add navigation to the TouchableOpacity ---这是我尝试向 TouchableOpacity 添加导航的部分

<TouchableOpacity
            style={styles.button}
            onPress={() => navigation.navigate("ViewDrafts")}
          >
            <Text style={styles.content}>View Drafts</Text>
          </TouchableOpacity>

error is undefined is not an object (evaluating 'navigation.navigate')错误undefined is not an object (evaluating 'navigation.navigate')

any idea?任何的想法? how to fix that?如何解决?

Try changing this:尝试改变这个:

navigation.navigate("ViewDrafts")

To this:对此:

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

If this is a functional component you need to include navigation , for example :如果这是一个功能组件,您需要包含navigation ,例如:

import React from 'react'
import { View, Text, TouchableOpacity } from 'react-native'

const Home = /*here*/({navigation}) =>(
    <View>
        <TouchableOpacity
            style={styles.button}
            onPress={() => navigation.navigate("ViewDrafts")}
          >
            <Text style={styles.content}>View Drafts</Text>
          </TouchableOpacity>
    </View>
)

export default Home

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

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