快速开始
只需安装react-navigation
的npm package, 就可以开始使用React Navigation了
使用 NPM 安装
npm install --save react-navigation
使用 Yarn 安装
yarn add react-navigation
要开始使用React Navigation,您必须先创建一个navigator,React Navigation带有三种默认的navigator。
StackNavigator
- 为应用程序提供了一种页面切换的方法,每次切换时,新的页面会放置在堆栈的顶部TabNavigator
- 用于设置具有多个Tab页的页面DrawerNavigator
- 用于设置抽屉导航的页面
创建 StackNavigator
StackNavigator
是最常见的一种navigator
,所以我们将用其进行基本演示。 首先,创建一个StackNavigator
。
import { StackNavigator } from 'react-navigation';
const RootNavigator = StackNavigator({
});
export default RootNavigator;
我们可以把页面添加到这个StackNavigator
. 每个key
代表着一个页面.
import React from 'react';
import { View, Text } from 'react-native';
import { StackNavigator } from 'react-navigation';
const HomeScreen = () => (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
</View>
);
const DetailsScreen = () => (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
</View>
);
const RootNavigator = StackNavigator({
Home: {
screen: HomeScreen,
},
Details: {
screen: DetailsScreen,
},
});
export default RootNavigator;
现在让我们为导航栏添加一个标题。
...
const RootNavigator = StackNavigator({
Home: {
screen: HomeScreen,
navigationOptions: {
headerTitle: 'Home',
},
},
Details: {
screen: DetailsScreen,
navigationOptions: {
headerTitle: 'Details',
},
},
});
export default RootNavigator;
最后,我们将能够从Home
页面跳转到Details
页面。 当你用一个navigator注册一个组件时,这个组件将会添加一个属性 navigation
。 这个属性能够控制不同页面间的跳转。
为了从Home
页面跳转到Details
页面,我们将使用 navigation.navigate
,方法如下:
...
import { View, Text, Button } from 'react-native';
const HomeScreen = ({ navigation }) => (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
onPress={() => navigation.navigate('Details')}
title="Go to details"
/>
</View>
);
...
懂了吧! 当然,这只是将StackNavigator和React Navigation作为一个整体使用的的基础。 这里是这个例子的完整代码:
import React from 'react';
import { View, Text, Button } from 'react-native';
import { StackNavigator } from 'react-navigation'; // 1.0.0-beta.14
const HomeScreen = ({ navigation }) => (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
onPress={() => navigation.navigate('Details')}
title="Go to details"
/>
</View>
);
const DetailsScreen = () => (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
</View>
);
const RootNavigator = StackNavigator({
Home: {
screen: HomeScreen,
navigationOptions: {
headerTitle: 'Home',
},
},
Details: {
screen: DetailsScreen,
navigationOptions: {
headerTitle: 'Details',
},
},
});
export default RootNavigator;
创建 TabNavigator
为了使用TabNavigator
,首先导入TabNavigator
并创建一个名为RootTabs
的组件
import { TabNavigator } from 'react-navigation';
const RootTabs = TabNavigator({
});
export default RootTabs;
然后我们需要创建一些页面,并将它们添加到我们创建的的TabNavigator
中。
import React from 'react';
import { View, Text } from 'react-native';
import { TabNavigator } from 'react-navigation';
const HomeScreen = () => (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
</View>
);
const ProfileScreen = () => (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Profile Screen</Text>
</View>
);
const RootTabs = TabNavigator({
Home: {
screen: HomeScreen,
},
Profile: {
screen: ProfileScreen,
},
});
export default RootTabs;
Getting there! Now let's explicity set a label and icon for the tab bar.
现在,我们给每个Tab Bar
设置一个明确的标签
我们将在例子中使用
react-native-vector-icons
, 如果你的项目中没有安装,请自行安装。
...
import Ionicons from 'react-native-vector-icons/Ionicons';
...
const RootTabs = TabNavigator({
Home: {
screen: HomeScreen,
navigationOptions: {
tabBarLabel: 'Home',
tabBarIcon: ({ tintColor, focused }) => (
<Ionicons
name={focused ? 'ios-home' : 'ios-home-outline'}
size={26}
style={{ color: tintColor }}
/>
),
},
},
Profile: {
screen: ProfileScreen,
navigationOptions: {
tabBarLabel: 'Profile',
tabBarIcon: ({ tintColor, focused }) => (
<Ionicons
name={focused ? 'ios-person' : 'ios-person-outline'}
size={26}
style={{ color: tintColor }}
/>
),
},
},
});
export default RootTabs;
这将确保tabBarLabel
是一致的(使用嵌套的navigator
时很重要),并且还会设置一个tabBarIcon
。 这个图标默认情况下会在iOS上默认显示,因为它使用了标签栏组件,这与Android上的标准设计模式一致。
你可以在查看下面的完整代码:
import React from 'react';
import { View, Text } from 'react-native';
import { TabNavigator } from 'react-navigation'; // 1.0.0-beta.14
import Ionicons from 'react-native-vector-icons/Ionicons'; // Supported builtin module
const HomeScreen = () => (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
</View>
);
const ProfileScreen = () => (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Profile Screen</Text>
</View>
);
const RootTabs = TabNavigator({
Home: {
screen: HomeScreen,
navigationOptions: {
tabBarLabel: 'Home',
tabBarIcon: ({ tintColor, focused }) => (
<Ionicons
name={focused ? 'ios-home' : 'ios-home-outline'}
size={26}
style={{ color: tintColor }}
/>
),
},
},
Profile: {
screen: ProfileScreen,
navigationOptions: {
tabBarLabel: 'Profile',
tabBarIcon: ({ tintColor, focused }) => (
<Ionicons
name={focused ? 'ios-person' : 'ios-person-outline'}
size={26}
style={{ color: tintColor }}
/>
),
},
},
});
export default RootTabs;
创建 DrawerNavigator
为了使用DrawerNavigator
,首先导入DrawerNavigator
并创建一个名为RootDrawer
的组件.
import { DrawerNavigator } from 'react-navigation';
const RootDrawer = DrawerNavigator({
});
export default RootDrawer;
然后我们需要创建一些页面,并将它们添加到我们创建的的DrawerNavigator
中。
import React from 'react';
import { View, Text } from 'react-native';
import { DrawerNavigator } from 'react-navigation';
const HomeScreen = () => (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
</View>
);
const ProfileScreen = () => (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Profile Screen</Text>
</View>
);
const RootDrawer = DrawerNavigator({
Home: {
screen: HomeScreen,
},
Profile: {
screen: ProfileScreen,
},
});
export default RootDrawer;
现在,我们给每个DrawerItem
设置一个明确的标签和图标
我们将在例子中使用
react-native-vector-icons
, 如果你的项目中没有安装,请自行安装。
...
import Ionicons from 'react-native-vector-icons/Ionicons';
...
const RootDrawer = DrawerNavigator({
Home: {
screen: HomeScreen,
navigationOptions: {
drawerLabel: 'Home',
drawerIcon: ({ tintColor, focused }) => (
<Ionicons
name={focused ? 'ios-home' : 'ios-home-outline'}
size={20}
style={{ color: tintColor }}
/>
),
},
},
Profile: {
screen: ProfileScreen,
navigationOptions: {
drawerLabel: 'Profile',
drawerIcon: ({ tintColor, focused }) => (
<Ionicons
name={focused ? 'ios-person' : 'ios-person-outline'}
size={20}
style={{ color: tintColor }}
/>
),
},
},
});
export default RootDrawer;
要打开抽屉,你可以从屏幕左侧向右滑动,也可以选择使用我们即将添加到Home
组件的方法navigation.navigate('DrawerToggle')
。
确保你已经从react-native
中导入了Button
组件。
...
const HomeScreen = ({ navigation }) => (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
onPress={() => navigation.navigate('DrawerToggle')}
title="Open Drawer"
/>
</View>
);
...
你可以查看下面的完整代码:
import React from 'react';
import { View, Text } from 'react-native';
import { DrawerNavigator } from 'react-navigation'; // 1.0.0-beta.14
import Ionicons from 'react-native-vector-icons/Ionicons'; // Supported builtin module
const HomeScreen = () => (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
</View>
);
const ProfileScreen = () => (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Profile Screen</Text>
</View>
);
const RootDrawer = DrawerNavigator({
Home: {
screen: HomeScreen,
navigationOptions: {
drawerLabel: 'Home',
drawerIcon: ({ tintColor, focused }) => (
<Ionicons
name={focused ? 'ios-home' : 'ios-home-outline'}
size={26}
style={{ color: tintColor }}
/>
),
},
},
Profile: {
screen: ProfileScreen,
navigationOptions: {
drawerLabel: 'Profile',
drawerIcon: ({ tintColor, focused }) => (
<Ionicons
name={focused ? 'ios-person' : 'ios-person-outline'}
size={26}
style={{ color: tintColor }}
/>
),
},
},
});
export default RootDrawer;