[英]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.当我搜索答案时,我不断看到有关屏幕的内容,但在我的情况下似乎不起作用。
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.