練習(xí)使用ReactNative的常用組件與自定義組件

通過使用TabBarIOS組件拓展其功能構(gòu)建了一個(gè)ReactNative項(xiàng)目低飒。包含內(nèi)容:熟練ReactNative的flex box布局,熟悉基本組件懂盐、自定義組件以及常用API 褥赊。

先看效果圖:


screen2



screen1



在index.ios.js中的主要代碼:

render: function(){
    return(
      <TabBarIOS style={styles.flex}>
        <TabBarIOS.Item
          title="首頁"
          onPress={this.select.bind(this, 'home')}
          selected={this.state.tab === 'home'}>
          <NavigatorIOS
            style={{flex:1}}
            initialRoute={{
              component: Home,
              title: '首頁',
              passProps: {},
            }}
          />
        </TabBarIOS.Item>

        <TabBarIOS.Item
          title="發(fā)現(xiàn)"
          onPress={this.select.bind(this, 'find')}
          selected={this.state.tab === 'find'}>
          <ScrollView style={styles.flex}>
          </ScrollView>     
        </TabBarIOS.Item>

        <TabBarIOS.Item
          title="我"
          onPress={this.select.bind(this, 'profile')}
          selected={this.state.tab === 'profile'}>
          <ScrollView style={styles.flex}>
          </ScrollView>
        </TabBarIOS.Item>
      </TabBarIOS>
    );
  }
});

這就是一個(gè)app的主要結(jié)構(gòu),在iOS中TabBarIOS充當(dāng)著UITabBarController的角色允粤。這里使用了自定義組件Home崭倘,需要導(dǎo)入var Home = require('./Home');

  • 自定義的首頁組件Home,在Home.js文件的主要代碼:
var Home = React.createClass({
  render: function(){
    return (
      <ScrollView style={styles.flex}>
        <Text style={styles.list_item} onPress={this.goTo}>☆ 點(diǎn)我喲1</Text>
        <Text style={styles.list_item} onPress={this.goTo}>☆ 點(diǎn)我喲2</Text>
        <Text style={styles.list_item} onPress={this.goTo}>☆ 點(diǎn)我喲3</Text>
      </ScrollView>
      );
  },
  goTo: function(){
    this.props.navigator.push({
      component: Detail,
      title: '詳情',
      rightButtonTitle: '右Button',
      onRightButtonPress: function(){
        alert('點(diǎn)擊了右Button');
      }
    });
  }
});
 module.exports = Home;

在iOS中相當(dāng)于ViewController类垫,也相當(dāng)于在Android中的Activity司光。在文件結(jié)尾特別注意這句代碼module.exports = Home;這是導(dǎo)出該類,才能讓別的類可引用悉患。這里用引用了Detail組件残家,在文件開頭需導(dǎo)入該類var Detail = require('./Detail');

  • 自定義的組件Detail主要代碼:
......
//首頁詳情
var Detail = React.createClass({
  render:function(){
    return (
      <ScrollView>
      <MargginBox>
        <BorderBox>
          <PaddingBox>
            <ElementBox>
            </ElementBox>
          </PaddingBox>
        </BorderBox>
      </MargginBox>
      </ScrollView>
    )
  }
});

var MargginBox = React.createClass({
  render:function(){
    return (
      <View style={[BoxStyles.margginBox]}>
        <Box  childName="borderBox"  height="height400" width="width400" boxName="margin" classBg="bgred">{this.props.children}</Box>
      </View>
    )
  }
});

var BorderBox = React.createClass({
  render:function(){
    return (
      <Box childName="paddingBox"  height="height300" width="width300" boxName="border" classBg="bggreen" >{this.props.children}</Box>
    )
  }
});

var PaddingBox = React.createClass({
  render:function(){
    return (
      <Box childName="elementBox"  height="height200" width="width200" boxName="padding" classBg="bgyellow" >{this.props.children}</Box>
    )
  }
});

var Box = React.createClass({
  render:function(){
    return (
      <View style={[BoxStyles.box,BoxStyles[this.props.width],BoxStyles[this.props.height]]}>
        <View  style={[BoxStyles.top,BoxStyles.height50,BoxStyles[this.props.classBg]]}><Text>top</Text></View>
        <View style={[BoxStyles[this.props.childName]]}>
          <View style={[BoxStyles.left,BoxStyles[this.props.classBg]]}><Text>left</Text></View>
            {this.props.children}
          <View style={[BoxStyles.right,BoxStyles[this.props.classBg]]}><Text>right</Text></View>
        </View>
        <View style={[BoxStyles.bottom,BoxStyles.height50,BoxStyles[this.props.classBg]]}><Text>bottom</Text></View>
        <View style={[BoxStyles.label]}><Text>{this.props.boxName}</Text></View>
      </View>
    )
  }
});

var ElementBox = React.createClass({
  render:function(){
    return (
      <View style={[BoxStyles.box,BoxStyles.height100]}>
        <View style={[BoxStyles.measureBox]}>
          <View style={[BoxStyles.right]}><Text>height</Text></View>
        </View>
        <View style={[BoxStyles.bottom,BoxStyles.height50]} ><Text>width</Text></View>
        <View style={[BoxStyles.label]}><Text>element</Text></View>
        <View style={[BoxStyles.widthdashed]}></View>
        <View style={[BoxStyles.heightdashed]}></View>
      </View>
    )
  }
});

var BoxStyles = StyleSheet.create({
......
})

 module.exports = Detail;    


特別注意:

1.在github下載了demo后直接運(yùn)行會(huì)報(bào)錯(cuò)

須cd到項(xiàng)目根目錄,然后在終端運(yùn)行npm install售躁,這會(huì)需要耐心等待一點(diǎn)時(shí)間坞淮。有如下的效果則成功,在配置中陪捷。

screen3.png

2.運(yùn)行到真機(jī)上的配置

jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"];

只需將AppDelegate.m文件上面代碼中的localhost替換為電腦ip地址即可回窘,如:192.168.1.10。

獲取電腦ip地址的簡單辦法市袖,如下圖啡直。

screen4.png



源碼請(qǐng)點(diǎn)擊github地址下載烁涌。


QQ:2239344645 我的github

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市酒觅,隨后出現(xiàn)的幾起案子撮执,更是在濱河造成了極大的恐慌,老刑警劉巖舷丹,帶你破解...
    沈念sama閱讀 216,544評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件抒钱,死亡現(xiàn)場離奇詭異,居然都是意外死亡颜凯,警方通過查閱死者的電腦和手機(jī)谋币,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來症概,“玉大人瑞信,你說我怎么就攤上這事⊙ㄔィ” “怎么了凡简?”我有些...
    開封第一講書人閱讀 162,764評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長精肃。 經(jīng)常有香客問我秤涩,道長,這世上最難降的妖魔是什么司抱? 我笑而不...
    開封第一講書人閱讀 58,193評(píng)論 1 292
  • 正文 為了忘掉前任筐眷,我火速辦了婚禮,結(jié)果婚禮上习柠,老公的妹妹穿的比我還像新娘匀谣。我一直安慰自己,他們只是感情好资溃,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,216評(píng)論 6 388
  • 文/花漫 我一把揭開白布武翎。 她就那樣靜靜地躺著,像睡著了一般溶锭。 火紅的嫁衣襯著肌膚如雪宝恶。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,182評(píng)論 1 299
  • 那天趴捅,我揣著相機(jī)與錄音垫毙,去河邊找鬼。 笑死拱绑,一個(gè)胖子當(dāng)著我的面吹牛综芥,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播猎拨,決...
    沈念sama閱讀 40,063評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼膀藐,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼征峦!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起消请,我...
    開封第一講書人閱讀 38,917評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎类腮,沒想到半個(gè)月后臊泰,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,329評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蚜枢,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,543評(píng)論 2 332
  • 正文 我和宋清朗相戀三年缸逃,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片厂抽。...
    茶點(diǎn)故事閱讀 39,722評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡需频,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出筷凤,到底是詐尸還是另有隱情昭殉,我是刑警寧澤,帶...
    沈念sama閱讀 35,425評(píng)論 5 343
  • 正文 年R本政府宣布藐守,位于F島的核電站挪丢,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏卢厂。R本人自食惡果不足惜乾蓬,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,019評(píng)論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望慎恒。 院中可真熱鬧任内,春花似錦、人聲如沸融柬。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽粒氧。三九已至越走,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間靠欢,已是汗流浹背廊敌。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留门怪,地道東北人骡澈。 一個(gè)月前我還...
    沈念sama閱讀 47,729評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像掷空,于是被迫代替她去往敵國和親肋殴。 傳聞我的和親對(duì)象是個(gè)殘疾皇子囤锉,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,614評(píng)論 2 353

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