Quick Start Guide - 快速入门指南

要开始使用React Navigation,你只需通过npm安装react-navigation包即可。

译者注:为了方便理解,下文中将所有screen翻译为页面

使用npm安装

  1. npm install --save react-navigation

使用Yarn安装

  1. yarn add react-navigation

要开始使用React Navigation,你必须创建一个导航器。React Navigation带有三个默认导航器。

  • StackNavigator - 为应用程序提供了一种在页面之间切换的方法,其中每个新页面都放置在堆栈的顶部。
  • TabNavigator - 用于设置带有多个tab的页面。
  • DrawerNavigator - 用于设置带抽屉导航的页面。

创建一个StackNavigator

StackNavigator是最常见的导航形式,所以我们将其用作基本演示。从创建一个StackNavigator开始。

  1. import { StackNavigator } from 'react-navigation';
  2. const RootNavigator = StackNavigator({
  3. });
  4. export default RootNavigator;

然后,我们可以为StackNavigator添加页面,每个键代表一个页面。

  1. import React from 'react';
  2. import { View, Text } from 'react-native';
  3. import { StackNavigator } from 'react-navigation';
  4. const HomeScreen = () => (
  5. <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
  6. <Text>Home Screen</Text>
  7. </View>
  8. );
  9. const DetailsScreen = () => (
  10. <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
  11. <Text>Details Screen</Text>
  12. </View>
  13. );
  14. const RootNavigator = StackNavigator({
  15. Home: {
  16. screen: HomeScreen,
  17. },
  18. Details: {
  19. screen: DetailsScreen,
  20. },
  21. });
  22. export default RootNavigator;

现在让我们为导航栏添加一个标题。

  1. ...
  2. const RootNavigator = StackNavigator({
  3. Home: {
  4. screen: HomeScreen,
  5. navigationOptions: {
  6. headerTitle: 'Home',
  7. },
  8. },
  9. Details: {
  10. screen: DetailsScreen,
  11. navigationOptions: {
  12. headerTitle: 'Details',
  13. },
  14. },
  15. });
  16. export default RootNavigator;

到此,我们应该能从Home页面导航到Details页面。当你使用导航器注册一个组件时,组件将会添加一个navigation属性。这个navigation属性驱动如何在不同页面之间切换。

要从Home页面切换到Details页面,我们将要使用navigation.navigate,如下所示:

  1. ...
  2. import { View, Text, Button } from 'react-native';
  3. const HomeScreen = ({ navigation }) => (
  4. <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
  5. <Text>Home Screen</Text>
  6. <Button
  7. onPress={() => navigation.navigate('Details')}
  8. title="Go to details"
  9. />
  10. </View>
  11. );
  12. ...

这样就可以了!这是使用StackNavigatorReact Navigation的一个整体基础。下面是这个例子的完整代码:

  1. import React from 'react';
  2. import { View, Text, Button } from 'react-native';
  3. import { StackNavigator } from 'react-navigation'; // 1.0.0-beta.14
  4. const HomeScreen = ({ navigation }) => (
  5. <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
  6. <Text>Home Screen</Text>
  7. <Button
  8. onPress={() => navigation.navigate('Details')}
  9. title="Go to details"
  10. />
  11. </View>
  12. );
  13. const DetailsScreen = () => (
  14. <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
  15. <Text>Details Screen</Text>
  16. </View>
  17. );
  18. const RootNavigator = StackNavigator({
  19. Home: {
  20. screen: HomeScreen,
  21. navigationOptions: {
  22. headerTitle: 'Home',
  23. },
  24. },
  25. Details: {
  26. screen: DetailsScreen,
  27. navigationOptions: {
  28. headerTitle: 'Details',
  29. },
  30. },
  31. });
  32. export default RootNavigator;

创建一个TabNavigator

要开始使用TabNavigator,首先要导入TabNavigator,并且创建一个新的RootTabs组件。

  1. import { TabNavigator } from 'react-navigation';
  2. const RootTabs = TabNavigator({
  3. });
  4. export default RootTabs;

然后,我们需要创建一些页面,并将其添加到我们的TabNavigator

  1. import React from 'react';
  2. import { View, Text } from 'react-native';
  3. import { TabNavigator } from 'react-navigation';
  4. const HomeScreen = () => (
  5. <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
  6. <Text>Home Screen</Text>
  7. </View>
  8. );
  9. const ProfileScreen = () => (
  10. <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
  11. <Text>Profile Screen</Text>
  12. </View>
  13. );
  14. const RootTabs = TabNavigator({
  15. Home: {
  16. screen: HomeScreen,
  17. },
  18. Profile: {
  19. screen: ProfileScreen,
  20. },
  21. });
  22. export default RootTabs;

快成功了!现在让我们来为标签栏设置一个标签和图标。

我们将在示例中使用react-native-vector-icons组件,如果你的项目中还没有安装这个组件,那么请安装它。

  1. ...
  2. import Ionicons from 'react-native-vector-icons/Ionicons';
  3. ...
  4. const RootTabs = TabNavigator({
  5. Home: {
  6. screen: HomeScreen,
  7. navigationOptions: {
  8. tabBarLabel: 'Home',
  9. tabBarIcon: ({ tintColor, focused }) => (
  10. <Ionicons
  11. name={focused ? 'ios-home' : 'ios-home-outline'}
  12. size={26}
  13. style={{ color: tintColor }}
  14. />
  15. ),
  16. },
  17. },
  18. Profile: {
  19. screen: ProfileScreen,
  20. navigationOptions: {
  21. tabBarLabel: 'Profile',
  22. tabBarIcon: ({ tintColor, focused }) => (
  23. <Ionicons
  24. name={focused ? 'ios-person' : 'ios-person-outline'}
  25. size={26}
  26. style={{ color: tintColor }}
  27. />
  28. ),
  29. },
  30. },
  31. });
  32. export default RootTabs;

这将确保tabBarLabel是一致的(使用嵌套的导航器时很重要),它会设置一个tabBarIcon。这个图标只有在使用标签栏组件时才能在iOS上默认可见,这与Android上的标准设计模式相一致。

你可以在下面查看完整的代码:

  1. import React from 'react';
  2. import { View, Text } from 'react-native';
  3. import { TabNavigator } from 'react-navigation'; // 1.0.0-beta.14
  4. import Ionicons from 'react-native-vector-icons/Ionicons'; // 4.4.2
  5. const HomeScreen = () => (
  6. <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
  7. <Text>Home Screen</Text>
  8. </View>
  9. );
  10. const ProfileScreen = () => (
  11. <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
  12. <Text>Profile Screen</Text>
  13. </View>
  14. );
  15. const RootTabs = TabNavigator({
  16. Home: {
  17. screen: HomeScreen,
  18. navigationOptions: {
  19. tabBarLabel: 'Home',
  20. tabBarIcon: ({ tintColor, focused }) => (
  21. <Ionicons
  22. name={focused ? 'ios-home' : 'ios-home-outline'}
  23. size={26}
  24. style={{ color: tintColor }}
  25. />
  26. ),
  27. },
  28. },
  29. Profile: {
  30. screen: ProfileScreen,
  31. navigationOptions: {
  32. tabBarLabel: 'Profile',
  33. tabBarIcon: ({ tintColor, focused }) => (
  34. <Ionicons
  35. name={focused ? 'ios-person' : 'ios-person-outline'}
  36. size={26}
  37. style={{ color: tintColor }}
  38. />
  39. ),
  40. },
  41. },
  42. });
  43. export default RootTabs;

创建一个DrawerNavigator

要开始使用DrawerNavigator,首先要导入DrawerNavigator,并且创建一个新的RootDrawer组件。

  1. import { DrawerNavigator } from 'react-navigation';
  2. const RootDrawer = DrawerNavigator({
  3. });
  4. export default RootDrawer;

然后,我们需要创建一些页面,并将其添加到我们的DrawerNavigator

  1. import React from 'react';
  2. import { View, Text } from 'react-native';
  3. import { DrawerNavigator } from 'react-navigation';
  4. const HomeScreen = () => (
  5. <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
  6. <Text>Home Screen</Text>
  7. </View>
  8. );
  9. const ProfileScreen = () => (
  10. <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
  11. <Text>Profile Screen</Text>
  12. </View>
  13. );
  14. const RootDrawer = DrawerNavigator({
  15. Home: {
  16. screen: HomeScreen,
  17. },
  18. Profile: {
  19. screen: ProfileScreen,
  20. },
  21. });
  22. export default RootDrawer;

快成功了!现在让我们来为标签栏设置一个标签和图标。

我们将在示例中使用react-native-vector-icons组件,如果你的项目中还没有安装这个组件,那么请安装它。

  1. ..
  2. import Ionicons from 'react-native-vector-icons/Ionicons';
  3. ...
  4. const RootDrawer = DrawerNavigator({
  5. Home: {
  6. screen: HomeScreen,
  7. navigationOptions: {
  8. drawerLabel: 'Home',
  9. drawerIcon: ({ tintColor, focused }) => (
  10. <Ionicons
  11. name={focused ? 'ios-home' : 'ios-home-outline'}
  12. size={20}
  13. style={{ color: tintColor }}
  14. />
  15. ),
  16. },
  17. },
  18. Profile: {
  19. screen: ProfileScreen,
  20. navigationOptions: {
  21. drawerLabel: 'Profile',
  22. drawerIcon: ({ tintColor, focused }) => (
  23. <Ionicons
  24. name={focused ? 'ios-person' : 'ios-person-outline'}
  25. size={20}
  26. style={{ color: tintColor }}
  27. />
  28. ),
  29. },
  30. },
  31. });
  32. export default RootDrawer;

要打开抽屉,你可以从屏幕左边缘向右滑动。你也可以通过navigation.navigate('DrawerToggle')选项打开抽屉视图,我们现在将打开选项添加到Home组件中。确保你已经从react-native中导入Button组件。

  1. ...
  2. const HomeScreen = ({ navigation }) => (
  3. <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
  4. <Text>Home Screen</Text>
  5. <Button
  6. onPress={() => navigation.navigate('DrawerToggle')}
  7. title="Open Drawer"
  8. />
  9. </View>
  10. );
  11. ...

你可以在下面查看已完成的代码。

  1. import React from 'react';
  2. import { View, Text } from 'react-native';
  3. import { DrawerNavigator } from 'react-navigation'; // 1.0.0-beta.14
  4. import Ionicons from 'react-native-vector-icons/Ionicons'; // 4.4.2
  5. const HomeScreen = () => (
  6. <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
  7. <Text>Home Screen</Text>
  8. </View>
  9. );
  10. const ProfileScreen = () => (
  11. <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
  12. <Text>Profile Screen</Text>
  13. </View>
  14. );
  15. const RootDrawer = DrawerNavigator({
  16. Home: {
  17. screen: HomeScreen,
  18. navigationOptions: {
  19. drawerLabel: 'Home',
  20. drawerIcon: ({ tintColor, focused }) => (
  21. <Ionicons
  22. name={focused ? 'ios-home' : 'ios-home-outline'}
  23. size={26}
  24. style={{ color: tintColor }}
  25. />
  26. ),
  27. },
  28. },
  29. Profile: {
  30. screen: ProfileScreen,
  31. navigationOptions: {
  32. drawerLabel: 'Profile',
  33. drawerIcon: ({ tintColor, focused }) => (
  34. <Ionicons
  35. name={focused ? 'ios-person' : 'ios-person-outline'}
  36. size={26}
  37. style={{ color: tintColor }}
  38. />
  39. ),
  40. },
  41. },
  42. });
  43. export default RootDrawer;