简体   繁体   English

如何使用反应导航导航到反应原生的新屏幕并且组件位于不同的文件中

[英]How to navigate to a new screen in react native using react navigation and the components are in a different file

I am planning to use React Navigation in my project to navigate within two screens.我计划在我的项目中使用 React Navigation 在两个屏幕内导航。

The index.js looks like index.js 看起来像

<NavigationContainer>
  <Stack.Navigator initialRouteName="Home">
    <Stack.Screen name="Home" component={HomeScreen} />
    <Stack.Screen name="Details" component={DetailsScreen} />
  </Stack.Navigator>
</NavigationContainer>

home.js contains a component called home and detailsScreen.js contains another component called detailsScreen. home.js 包含一个名为 home 的组件,而 detailsScreen.js 包含另一个名为 detailsScreen 的组件。

  1. What should I do to navigate to detailsScreen from Home?我应该怎么做才能从 Home 导航到 detailsScreen? Do I have to import/export anything?我必须导入/导出任何东西吗? If yes, where?如果是,在哪里? The official docs ( https://reactnavigation.org/docs/navigating/ ) only consider the scenario where all components are in the same file.官方文档( https://reactnavigation.org/docs/navigating/ )只考虑了所有组件都在同一个文件中的场景。

  2. Can I call navigation.navigate in the following way?我可以通过以下方式调用 navigation.navigate 吗?

return (
  <View>
    {navigation.navigate('Details')}
  </View>
)

You can navigate the any screen by navigate method.您可以通过navigate方法导航任何屏幕。 Just make sure that the screen is added in your stack.只需确保屏幕已添加到您的堆栈中。 For navigation, just follow navigation.navigate('Details')对于导航,只需按照navigation.navigate('Details')

For accessing navigation in your component.You can pass the prop function YourComponent({ navigation }){用于访问组件中的navigation 。您可以传递道具 function YourComponent({ navigation }){

//use navigation
//navigation.navigate("Details")
}

Please follow Doc请按照文档

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

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