React Navigation 使用

React Navigation

React Navigation 分為stack navigator,tab navigation屉来, drawer navigation。能夠提供原生的手勢(shì)和動(dòng)畫效果狈癞。

stack navigator

createStackNavigator 返回一個(gè) React組件茄靠,如果是單獨(dú)的導(dǎo)航欄控制器,可以視為rootController蝶桶。createStackNavigator是一個(gè)函數(shù)慨绳,接收一個(gè)路由對(duì)象和optional config 對(duì)象,router對(duì)象key->value;

// app.js 文件
import React from 'react';
import { View, Text } from 'react-native';
import { createStackNavigator } from 'react-navigation';

class HomeScreen extends React.Component {
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Home Screen</Text>
      </View>
    );
  }
}
// 導(dǎo)出模塊
export default createStackNavigator({
  Home: {
      screen: HomeScreen,
      navigationOptions: () => ({
        title: 'Home',
    }),
  },
});

// index.js 中注冊(cè)組件
AppRegistry.registerComponent('ReactApp', () => App);

router跳轉(zhuǎn)真竖,需要聲明router對(duì)象,可以傳遞參數(shù),通過getParam接收

class HomeScreen extends React.Component {
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Home Screen</Text>
        <Button
          title = "jump to Mine"
          onPress={() => this.props.navigation.navigate('Mine',{
                id:100
           })}
        />
      </View>
    );
  }
}

class MineScreen extends React.Component {
  render() {
    let {navigation} = this.props;
    let cusId = navigation.getParam('id','cusId')
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Mine Screen</Text>
        <Text>傳遞ID參數(shù): {cusId}</Text>
      </View>
    );
  }
}

export default createStackNavigator({
  Home: {
    screen: HomeScreen,
  },
  Mine: {
    screen: MineScreen,
  }
},{
  initialRouteName: 'Home'
});

navigation標(biāo)題 樣式設(shè)置脐雪,如何不覆蓋,會(huì)共享navigationOptions,

class MineScreen extends React.Component {
  static navigationOptions = {
    title: 'MineScreen',
  };
  static navigationOptions = ({ navigation }) => {
    return {
      // 標(biāo)題
      title: navigation.getParam('otherParam'),
      headerTintColor: 'blue',
      headerTitleStyle: {
        fontWeight: 'bold',
      },
      // left barItem
      headerRight: (
        <Button
          onPress={() => alert('This is a button!')}
          title="Info"
          color= "blue"
        />
      ),
    };
  };
   
  // 
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Mine Screen</Text>
      </View>
    );
  }
}

Tab navigation

生成簡(jiǎn)單的tabbar,通過navigationOptions設(shè)置底部tabnav恢共,tabBarIcon也是一個(gè)又返回值的函數(shù)战秋,tabBarOptions設(shè)置item屬性


import React from 'react';
import { View, Text, Button, Image, Icon, StyleSheet, SafeAreaView,StatusBar } from 'react-native';
import { createStackNavigator,NavigationComponent,createBottomTabNavigator } from 'react-navigation';


// 首頁
class HomeScreen extends React.Component {
  static navigationOptions =  {
    title: "Home"
  }
  render() {
    return (
      <SafeAreaView style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Home Screen</Text>
        <Button
          title = "jump to Mine"
          onPress={() => this.props.navigation.push('Details',{
            id:1
          })}
        />
      </SafeAreaView>
    );
  }
}
// 跳轉(zhuǎn)頁 details
class DetailsScreen extends React.Component {
  static navigationOptions = () => {
    return {
      // 標(biāo)題
      title: "Details",
    }
  }

  render() {
      
    let { navigation } = this.props;
     // 傳遞值
    let cusId = navigation.getParam('id', 'cusId')
    

    return (
      // ios 安全區(qū)域
      <SafeAreaView style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <StatusBar
          barStyle="dark-content"
          backgroundColor="blue"
        />
        <Text>Mine Screen</Text>
        <Text>傳遞ID參數(shù): {cusId}</Text>
      </SafeAreaView>
    );
  }
}
// 我的
class MineScreen extends React.Component {

  static navigationOptions = ({ navigation }) => {
    return {
      // 標(biāo)題
      title: 'Mine',
      headerTintColor: 'blue',
      headerTitleStyle: {
        fontWeight: 'bold',
      },
      // left barItem
      headerRight: (
        <Button
          onPress={() => alert('This is a button!')}  title="Info" color= "blue"
        />
      ),
    };
  };

  // 組件掛載完成后,改動(dòng)navigation旁振,
  // componentDidMount() {
  //   this.props.navigation.setParams({ increaseCount: this._increaseCount });
  // }

  render() {
    let {navigation} = this.props;
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Mine Screen</Text>
      </View>
    );
  }
}

// 導(dǎo)航欄1
const nav1 =  createStackNavigator({
  Home: {
    screen: HomeScreen,
    navigationOptions: () => ({
      title: 'Home',
      tabBarComponent:null
    },{
      mode: 'card',
      navigationOptions: {
        gesturesEnabled: true,
      },
    }),
  },
  Details: {
    screen: DetailsScreen,
  }
},{
  initialRouteName: 'Home'
});

//導(dǎo)航欄2
const nav2 = createStackNavigator({

  Mine: {
      screen: MineScreen,
    }
  }, {
    initialRouteName: 'Mine'
  });


export default createBottomTabNavigator(
  {
    Home:nav1,
    Mine: nav2
  },
  {
    // tabbar 配置
    navigationOptions: ({ navigation }) => ({
      tabBarLabel: navigation.state.routeName,
      tabBarVisible: navigation.state.index>0 ? false:true, // 顯示tabbar
      tabBarIcon: ({ tintColor,focused }) => {
        
        let routeName = navigation.state.routeName
        let soucrePath
        if (routeName === 'Home') {

          soucrePath = focused ? imageArr[1] : imageArr[0]
        } else if (routeName === 'Mine') {

          soucrePath = focused ? imageArr[3] : imageArr[2]
        }
        
        return <Image style={[{ width: 24, height: 24 }]} source={soucrePath} />
      },
    }),
    tabBarOptions: {
      showIcon: true
    },
  }
);

const imageArr = [
  require('./src/res/images/home_n.png'),
  require('./src/res/images/home_s.png'),
  require('./src/res/images/mine_n.png'),
  require('./src/res/images/mine_s.png')
]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末获询,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子拐袜,更是在濱河造成了極大的恐慌吉嚣,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,755評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蹬铺,死亡現(xiàn)場(chǎng)離奇詭異尝哆,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)甜攀,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門秋泄,熙熙樓的掌柜王于貴愁眉苦臉地迎上來琐馆,“玉大人,你說我怎么就攤上這事恒序∈蒴铮” “怎么了?”我有些...
    開封第一講書人閱讀 165,138評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵歧胁,是天一觀的道長(zhǎng)滋饲。 經(jīng)常有香客問我,道長(zhǎng)喊巍,這世上最難降的妖魔是什么屠缭? 我笑而不...
    開封第一講書人閱讀 58,791評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮崭参,結(jié)果婚禮上呵曹,老公的妹妹穿的比我還像新娘。我一直安慰自己何暮,他們只是感情好奄喂,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著郭卫,像睡著了一般砍聊。 火紅的嫁衣襯著肌膚如雪背稼。 梳的紋絲不亂的頭發(fā)上贰军,一...
    開封第一講書人閱讀 51,631評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音蟹肘,去河邊找鬼词疼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛帘腹,可吹牛的內(nèi)容都是我干的贰盗。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼阳欲,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼舵盈!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起球化,我...
    開封第一講書人閱讀 39,264評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤秽晚,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后筒愚,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體赴蝇,經(jīng)...
    沈念sama閱讀 45,724評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年巢掺,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了句伶。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片劲蜻。...
    茶點(diǎn)故事閱讀 40,040評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖考余,靈堂內(nèi)的尸體忽然破棺而出先嬉,到底是詐尸還是另有隱情,我是刑警寧澤楚堤,帶...
    沈念sama閱讀 35,742評(píng)論 5 346
  • 正文 年R本政府宣布坝初,位于F島的核電站,受9級(jí)特大地震影響钾军,放射性物質(zhì)發(fā)生泄漏鳄袍。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評(píng)論 3 330
  • 文/蒙蒙 一吏恭、第九天 我趴在偏房一處隱蔽的房頂上張望拗小。 院中可真熱鬧,春花似錦樱哼、人聲如沸哀九。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽阅束。三九已至,卻和暖如春茄唐,著一層夾襖步出監(jiān)牢的瞬間息裸,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評(píng)論 1 270
  • 我被黑心中介騙來泰國(guó)打工沪编, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留呼盆,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,247評(píng)論 3 371
  • 正文 我出身青樓蚁廓,卻偏偏與公主長(zhǎng)得像访圃,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子相嵌,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容