简体   繁体   English

React Native Navigation:在另一个文件的函数内部时无法在堆栈之间导航

[英]React Native Navigation : can't navigate between stack when inside a function from another file

I'm having a bit of problem with how navigation works in React Native .我对导航React Native 中的工作方式有一些问题。
Basically what I'm trying to do is this :\\基本上我想做的是这样的:\\

While I'm in the Home stack navigate to the SubFeed stack from the Feed function called in Home .虽然我在主页堆栈导航到称为首页进纸功能,子馈电堆。

Here is my code这是我的代码

function Home({ navigation }) {

return (
    <ScrollView>
        <Feed/>
    </ScrollView>

Home.js主页.js

function Feed({ navigation }) {

return (
    <View>
        <Button title="nav1" 
                onPress={() => navigation.navigate('SubFeed')}/>
        <Button title="nav2" 
                onPress={() => navigation.navigate('Home', {screen: 'SubFeed'})}/>
    </View>

Feed.js Feed.js


The problem is that whenever I try to press either of the buttons I keep getting the same error:问题是,每当我尝试按下任何一个按钮时,我都会收到相同的错误:

TypeError: undefined is not an object (evaluating 'navigation.navigate') \\ TypeError: undefined is not an object (evaluating 'navigation.navigate') \\

When I search for an answer I keep seeing things about screens but it doesn't seem to be working in my case.当我搜索答案时,我不断看到有关屏幕的内容,但在我的情况下似乎不起作用。

  • I think the problem comes from the fact that I try to change the stack outside from home, but I can't understand why.我认为问题出在我试图从家里改变堆栈之外的事实,但我不明白为什么。

If you have any suggestion or even better the answer to my problem I would really appreciate it.如果您有任何建议,或者甚至更好地回答我的问题,我将不胜感激。

In your code, Feed component doesn't know about navigation property.在您的代码中,Feed 组件不知道导航属性。 So, you can pass it as a prop form the parent Home or you can use react-navigation-hooks and use const { navigate } = useNavigation();因此,您可以将其作为来自父 Home 的道具传递,或者您可以使用react-navigation-hooks并使用const { navigate } = useNavigation(); inside Feed component.在 Feed 组件内。

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

相关问题 无法使用 React Native 从 Navigation 5 中的抽屉内部导航 - Can't navigate from inside drawer in Navigation 5 using React Native 无法从 header React Native Navigation 导航 - Can't navigate from header React Native Navigation 在React Native中如何在重置导航堆栈的同时从子Stack Navigator导航回父项 - How to navigate from a child Stack Navigator back to parent while resetting navigation stack at the same time, in React Native 无法在反应导航堆栈中居中标题 - React-Native? - Can't center title in react navigation stack - React-Native? 使用 react-native-navigation 导航到另一个屏幕 - Navigate to another screen with react-native-navigation 尝试在两个站点 React Native 之间导航时,未定义不是对象(评估“navigation.navigate”) - Undefined is not an object (evaluating 'navigation.navigate') when trying to navigate between two sites React Native 如何在不使用 navigation.navigate() 的情况下在 React Native 中的屏幕之间导航? - How can I navigate between screens in React Native without using navigation.navigate()? 反应本机-navigation.navigate不是函数 - React native - navigation.navigate is not a function React Native-从另一个文件导航 - React Native - Navigation from another file 如何在反应导航中从抽屉堆栈导航到开关堆栈? - How can i navigate from a Drawer Stack to a Switch Stack in react navigation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM