React Navigation

React Navigation - 使用说明

开始使用React Navigation,必须先创建一个navigator,React Navigation带有三种默认的navigator。

  1. StackNavigator - 为应用程序提供了一种页面切换的方法,每次切换时,新的页面会放置在堆栈的顶部
  2. TabNavigator - 用于设置具有多个Tab页的页面
  3. DrawerNavigator - 用于设置抽屉导航的页面

StackNavigator:实现不同页面之间的跳转

<Stack.Navigator
    screenOptions={{
        headerStyle: {
        backgroundColor: '#f4511e',
        },
        // headerBackVisible: false,
        //隐藏顶部导航栏:headerShown: false,
        headerTitleAlign: 'center',
        headerTintColor: '#fff',
        headerTitleStyle: {
          fontWeight: 'bold',
        },
    }} >
    <Stack.Screen
        name="home"
        component={Home}
        options={{
          title: '首页'
        }}
      />
      <Stack.Screen
        name="detail"
        component={Detail}
      />
    </Stack.Navigator>

页面跳转及参数传递:

navigation.navigate('detail',{userName:'张三'})

使用params:

options={({ route }) => ({ title: route.params.name })}
//title 必须是字符串类型

setOptions:

navigation.setOptions({ title: 'Updated!' })

screenOptions:

通用样式往可以往父级加screenOptions属性

TabNavigator:用导航栏实现不同页面间的跳转

TabNavigator生成的底部导航的结构包括选项卡栏,选项卡,图标以及标签文本四部分。

//用于显示或隐藏标签栏
const screens=['home','set','userinfo']
const tabbarIsVisible = (route) => {
  const routeName = getFocusedRouteNameFromRoute(route)
  return screens.includes(routeName)
}
<Tab.Navigator
      screenOptions={{
        headerShown: false,
      }}>
        <Tab.Screen name="homeScreen" component={HomeScreen}
          options={({route}) => {
            return {
              title: '首页',
              //选项卡,自定义图标
              tabBarIcon: ({ color, size }) => <Ionicons name="home" size={size} color={color} />,
              tabBarStyle:{display:tabbarIsVisible(route) ? 'block' : 'none'}
            }
          }} />
        <Tab.Screen name="setting" component={Setting}
          options={{
            title: '设置',
            tabBarIcon: ({ color, size }) => <Ionicons name="settings" size={size} color={color} />,
          }} />
      </Tab.Navigator>

DrawerNavigator:从侧边栏划出抽屉一样的效果

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值