React Native (一)

創(chuàng)建一個導(dǎo)航器

這個導(dǎo)航器實現(xiàn)的功能類似于瀏覽器的前進(jìn)后退跳轉(zhuǎn)褥符,沒有tab導(dǎo)航龙誊。

創(chuàng)建一堆頁面用來跳轉(zhuǎn)

首先創(chuàng)建幾個頁面


// as long as you use react native related component you need next line
import React, { Component } from 'react'
import {View, Text, StyleSheet, Button} from 'react-native'
/*
in object this.props, navigation is very important, go(), navigate() often use
navigation = state + setParams + other
params is in state
so you may write this code frequently

const {navigation} = this.props
const {setParams, state} = navigation
const {params} = state
*/
export default class Home extends Component{
  static navigationOptions = {
    title: "Home",
    headerBackTitle: "回首頁"
  }
  render(){
    const {navigation} = this.props
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>Welcome to Home Page!</Text>
        <Button title={'GO Page 1'} onPress={() => navigation.navigate("Page1","Dynamic Page Name")}/>
        <Button title={'GO Page 2'} onPress={() => navigation.navigate("Page2","Dynamic Page Name")}/>
        <Button title={'GO Page 3'} onPress={() => navigation.navigate("Page3","Dynamic Page Name")}/>
      </View>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  }
});

其他的頁面類似。

創(chuàng)建導(dǎo)航

// to create a navigator, these code is necessary
import React, {Component} from "react"
import {View, Text, Button} from 'react-native'
import {createStackNavigator, createAppContainer} from "react-navigation"

// then, import your pages
import HomePage from '../pages/Home.js'
import Page1 from '../pages/Page1.js'
import Page2 from '../pages/Page2.js'
import Page3 from '../pages/Page3.js'
import Page4 from '../pages/Page4.js'

// create a navigator by createStackNavigator, means stack router
// I create three pages use none config, dynamic config and static config
// this api need a config object like this:

const AppNavigator = createStackNavigator({
  Home: {
    screen: HomePage,
    navigationOptions: {}
    // you can set navigationOptions at here
    // you can set navigationOptions at HomePage also
  },
  Page1: {
    screen: Page1,
    navigationOptions: ({ navigation } ) => { // navigation was in props, take it out by use deconstruction assignment
      title: `${navigation.state.params.name}` // dynamic navigation data
    }
  },
  Page2: {
    screen: Page2,
    // here can static appoint what the title it show
    navigationOptions: {
      title: "This is Page2" // static navigation data
    }
  },
  Page3: {
    screen: Page3,
    navigationOptions: (props) => {
      const { navigation } = props
      const { state, setParams } = navigation
      const { params } = state
      return {
        title: params.title ? params.title : "This is Page3",
        headerRight: (
          <Button
              title={params.mode === 'edit' ? '保存' : '編輯'}
              onPress={() => {setParams({mode: params.mode === 'edit' ? '':'edit'})}}/>
        )
      }
    }
  },
  Page4: {
    screen: Page4,
    navigationOptions: {
      title: "This is Page4"
    }
  }
}, {
  initialRouteName: "Home" // initialRouteName as first Page you launch
})


/* 
  pay attention to Page3, the title depends on this line of code
  `title: params.title ? params.title : "This is Page3"`
  that means if params.title is not null, the title will be params.title
  it's to say, when I call setParams({title: "what I want"})
  the title of Page3 will be change.
*/

接下來的事情就是喷楣, 將這個導(dǎo)航器導(dǎo)出趟大,在index.js中導(dǎo)入并且使用。

// must do this, do not ask me why
const AppContainer = createAppContainer(AppNavigator)
export default AppContainer

然后抡蛙, 在index.js里面

// ./navigator/navigator.js is our navogator, that is the above code
import {AppRegistry} from 'react-native';
// import App from './App';
import App from './navigator/navigator.js'
import {name as appName} from './app.json';

AppRegistry.registerComponent(appName, () => App);

react-native run-ios. 神奇的事情發(fā)生了护昧。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市粗截,隨后出現(xiàn)的幾起案子惋耙,更是在濱河造成了極大的恐慌,老刑警劉巖熊昌,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件绽榛,死亡現(xiàn)場離奇詭異,居然都是意外死亡婿屹,警方通過查閱死者的電腦和手機灭美,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來昂利,“玉大人届腐,你說我怎么就攤上這事》浼椋” “怎么了犁苏?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長扩所。 經(jīng)常有香客問我围详,道長,這世上最難降的妖魔是什么祖屏? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任助赞,我火速辦了婚禮,結(jié)果婚禮上袁勺,老公的妹妹穿的比我還像新娘雹食。我一直安慰自己,他們只是感情好期丰,可當(dāng)我...
    茶點故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布群叶。 她就那樣靜靜地躺著漠嵌,像睡著了一般。 火紅的嫁衣襯著肌膚如雪盖呼。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天化撕,我揣著相機與錄音几晤,去河邊找鬼。 笑死植阴,一個胖子當(dāng)著我的面吹牛蟹瘾,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播掠手,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼憾朴,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了喷鸽?” 一聲冷哼從身側(cè)響起众雷,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎做祝,沒想到半個月后砾省,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡混槐,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年编兄,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片声登。...
    茶點故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡狠鸳,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出悯嗓,到底是詐尸還是另有隱情件舵,我是刑警寧澤,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布绅作,位于F島的核電站芦圾,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏俄认。R本人自食惡果不足惜个少,卻給世界環(huán)境...
    茶點故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望眯杏。 院中可真熱鬧夜焦,春花似錦、人聲如沸岂贩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至卸伞,卻和暖如春抹镊,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背荤傲。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工垮耳, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人遂黍。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓终佛,卻偏偏與公主長得像,于是被迫代替她去往敵國和親雾家。 傳聞我的和親對象是個殘疾皇子铃彰,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,871評論 2 354

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