转载链接:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/react-native-component-packaging-and-traditional-values/

刚接触React-Native的时候也是看官方文档,官方文档就是讲的基础的组件与与基础的API,然后就开始写一些简单的界面,但是发现自己写的简单界面代码非常的长,修改起来也是非常的麻烦,维护起来非常的费尽。那么今天就简单的介绍一下组件的封装和传值吧。你会发现节省了好多的代码。

效果图:(如下所示)

一、先说说没有封装之前的代码是什么样子的吧。

'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
PixelRatio,
} = React;
var stylesForXC = StyleSheet.create({
container : {
height: 84,
borderWidth:1,
borderColor: 'black',
flexDirection: 'row',
marginTop: 25,
marginLeft: 5,
marginRight: 5,
borderRadius: 5, /*圆角半径*/
padding: 2,
backgroundColor: '#949494'
}, item: {
flex: 1,
height: 80
}, flex: {
flex: 1
}, center: {
justifyContent: 'center',/*垂直水平居中,其实是按照flexDriection的方向居中*/
alignItems : 'center', /*水平居中*/
}, font : {
color: '#fff',
fontSize: 16,
fontWeight: 'bold',
}, lineLeft: {
borderLeftWidth: 1/PixelRatio.get(),
borderColor: '#fff',
}, lineCenter: {
borderBottomWidth:1/PixelRatio.get(),
borderColor: '#fff',
}
}) 'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
PixelRatio,
} = React;
var stylesForXC = StyleSheet.create({
container : {
height: 84,
borderWidth:1,
borderColor: '#949494',
flexDirection: 'row',
marginTop: 25,
marginLeft: 5,
marginRight: 5,
borderRadius: 5, /*圆角半径*/
padding: 2,
backgroundColor: '#949494',
}, item: {
flex: 1,
height: 80,
}, flex: {
flex: 1,
}, center: {
justifyContent: 'center',/*垂直水平居中,其实是按照flexDriection的方向居中*/
alignItems : 'center', /*水平居中*/
}, font : {
color: '#fff',
fontSize: 16,
fontWeight: 'bold',
}, lineLeft: {
borderLeftWidth: 1/PixelRatio.get(),
borderColor: '#fff',
}, lineCenter: {
borderBottomWidth:1/PixelRatio.get(),
borderColor: '#fff',
}
}) AppRegistry.registerComponent('wxsPrj', () => wxsPrj);
var betree2 = React.createClass({
render: function() {
return (
<View style = {stylesForXC.flex}>
<View style = {[stylesForXC.container]}>
<View style = {[stylesForXC.item,stylesForXC.center]}>
<Text style= {stylesForXC.font}>饭馆</Text>
</View> <View style = {[stylesForXC.item,stylesForXC.lineLeft]}>
<View style = {[stylesForXC.center,stylesForXC.flex,stylesForXC.lineCenter]}>
<Text style= {stylesForXC.font}>服装城</Text>
</View>
<View style = {[stylesForXC.center,stylesForXC.flex]}>
<Text style= {stylesForXC.font}>美食街</Text>
</View>
</View> <View style = {[stylesForXC.item,stylesForXC.lineLeft]}>
<View style = {[stylesForXC.center,stylesForXC.flex,stylesForXC.lineCenter]}>
<Text style= {stylesForXC.font}>电脑城</Text>
</View>
<View style = {[stylesForXC.center,stylesForXC.flex]}>
<Text style= {stylesForXC.font}>全球购</Text>
</View>
</View>
</View>
</View>
);
}
}) AppRegistry.registerComponent('betree2', () => betree2);

我们发现在主函数上界面布局很多,这样不利于模块化的思想,我们其实可以把里面的界面的布局封装成一个名为Box的组件,然后在主函数中对组件进行引用,这样看起来就好多了。

二、封装组件后的代码如下:

render:function(){
return(
<View style = {stylesForXC.flex}>
<View style = {[stylesForXC.container]}>
<View style = {[stylesForXC.item,stylesForXC.center]}>
<Text style= {stylesForXC.font}>饭馆</Text>
</View> <View style = {[stylesForXC.item,stylesForXC.lineLeft]}>
<View style = {[stylesForXC.center,stylesForXC.flex,stylesForXC.lineCenter]}>
<Text style= {stylesForXC.font}>服装城</Text>
</View>
<View style = {[stylesForXC.center,stylesForXC.flex]}>
<Text style= {stylesForXC.font}>美食街</Text>
</View>
</View> <View style = {[stylesForXC.item,stylesForXC.lineLeft]}>
<View style = {[stylesForXC.center,stylesForXC.flex,stylesForXC.lineCenter]}>
<Text style= {stylesForXC.font}>电脑城</Text>
</View>
<View style = {[stylesForXC.center,stylesForXC.flex]}>
<Text style= {stylesForXC.font}>全球购</Text>
</View>
</View>
</View>
</View>
);
}
}) var betree2 = React.createClass({
render: function() {
return (
<Box></Box>
);
}
})

这样看起来把布局放进去,在主函数中调用就可以了,这样是不是就清晰很多了。有一点我们是需要注意的就是:这种定义的组件首字母一定要大写,不然会报错(如下图所示,意思就是说每个首字母的名字要大写,刚开始我也没注意这个细节)。

三、那么问题又来了,如果我想修改<Text>组件里面的内容(比如:'全球购'改为'电脑馆'),有人会说那好办自己找下里面的代码把''全球购'改为'电脑馆'不就可以了,那如果我成百个Text呢? 我们其实可以定义一个组件参数,这样就方便多了。代码如下:

var Box = React.createClass({
render:function(){
return(
<View style = {stylesForXC.flex}>
<View style = {[stylesForXC.container]}>
<View style = {[stylesForXC.item,stylesForXC.center]}>
<Text style= {stylesForXC.font}>{this.props.one}</Text>
</View> <View style = {[stylesForXC.item,stylesForXC.lineLeft]}>
<View style = {[stylesForXC.center,stylesForXC.flex,stylesForXC.lineCenter]}>
<Text style= {stylesForXC.font}>{this.props.second1}</Text>
</View>
<View style = {[stylesForXC.center,stylesForXC.flex]}>
<Text style= {stylesForXC.font}>{this.props.second2}</Text>
</View>
</View> <View style = {[stylesForXC.item,stylesForXC.lineLeft]}>
<View style = {[stylesForXC.center,stylesForXC.flex,stylesForXC.lineCenter]}>
<Text style= {stylesForXC.font}>{this.props.third1}</Text>
</View>
<View style = {[stylesForXC.center,stylesForXC.flex]}>
<Text style= {stylesForXC.font}>{this.props.third2}</Text>
</View>
</View>
</View>
</View>
);
}
}) var betree2 = React.createClass({
render: function() {
return ( <Box one = "饭馆" second1 = "服装城" second2 = "美食街" third1 = "电脑城" third2 = "全球购"></Box> );
}
})
效果图如下所示:

react-native组件封装与传值的更多相关文章

  1. beeshell —— 开源的 React Native 组件库

    介绍 beeshell 是一个 React Native 应用的基础组件库,基于 0.53.3 版本,提供一整套开箱即用的高质量组件,包含 JavaScript(以下简称 JS)组件和复合组件(包含 ...

  2. react native组件的创建

    react native组件的创建 react文件加载顺序: react项目启动后,先加载index.js.在index.js中可以指向首页. import { AppRegistry } from ...

  3. React Native组件之Text

    React Native组件之Text相当于iOS中的UILabel. 其基本属性如下: /** * Sample React Native App * https://github.com/face ...

  4. React Native组件之Switch和Picker和Slide

    React Native组件Switch类似于iOS中的UISwitch:组件Slide类似于iOS中UIslider,组件Picker类似于iOS的UIPickerView.他们的使用方法和相关属性 ...

  5. React Native 组件之TextInput

    React Native 组件之TextInput类似于iOS中的UITextView或者UITextField,是作为一个文字输入的组件,下面的TextInput的用法和相关属性. /** * Sa ...

  6. react native组件的生命周期

    react native组件的生命周期 一.当页面第一次加载时,会依次调用: constructor() componentWillMount(): 这个函数调用时机是在组件创建,并初始化了状态之后, ...

  7. React Native组件(三)Text组件解析

    相关文章 React Native探索系列 React Native组件系列 前言 此前介绍了最基本的View组件,接下来就是最常用的Text组件,对于Text组件的一些常用属性,这篇文章会给出简单的 ...

  8. React Native组件(二)View组件解析

    相关文章 React Native探索系列 React Native组件系列 前言 了解了RN的组件的生命周期后,我们接着来学习RN的具体的组件.View组件是最基本的组件,也是首先要掌握的组件,这一 ...

  9. Android React Native组件的生命周期及回调函数

    熟悉android的童鞋应该都清楚,android是有生命周期的,其很多组件也是有生命周期.今天小编和大家分享的React Native组件的生命周期,还不了解的童鞋,赶紧来围观吧 在android开 ...

  10. React Native组件解析(二)之Text

    React Native组件解析(二)之Text 1. 概述 Text组件对应于iOS的UILabel,Android的TextView,用来显示文本.但是Text组件的内部使用的并不是flexbox ...

随机推荐

  1. 017_nginx重定向需求

    重定向的各种需求 需求一. 前端同事需要把特定的url进行重定向,实现如下: location / { root /data/base.apiportal_opsweb; index index.ht ...

  2. PYTHON-匿名函数,递归与二分法,面向过程编程

    """匿名函数1 什么是匿名函数 def定义的是有名函数:特点是可以通过名字重复调用 def func(): #func=函数的内存地址 pass 匿名函数就是没有名字的 ...

  3. Linux 文件查找命令详解

    find命令 Linux find命令用来在指定目录下查找文件.任何位于参数之前的字符串都将被视为欲查找的目录名.如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件.并且将 ...

  4. JUnit单元测试--IntelliJ IDEA

    单元测试的基本使用 一.环境配置 使用idea IDE 进行单元测试,首先需要安装JUnit 插件. 1.安装JUnit插件步骤 File-->settings-->Plguins--&g ...

  5. notepad++ 快捷键大全、notepad常用快捷键

    Notepad++ 快捷键 大全, notepad++也情有独钟,最近发现了一个快捷键,就是选中单词,ctrl+shift+enter.不过现在想知道一个快捷键,假设有三行代码,选中后一般按TAB就可 ...

  6. TCP/IP、Http大纲

    TPC/IP协议是传输层协议,主要解决数据如何在网络中传输,而HTTP是应用层协议,主要解决如何包装数据.关于TCP/IP和HTTP协议的关系,网络有一段比较容易理解的介绍:“我们在传输数据时,可以只 ...

  7. Yahoo的Yslow23条规则

    一. CONTENT 减少使用HTTP请求(Minimize HTTP Requests) 通常打开一个页面的时候,大部分的时间都是在下载该页面的图片,css样式,js脚本,flash等等资源,减少这 ...

  8. Linux学习笔记:使用shell脚本实现ftp的自动上传下载

    在 Linux 下可以利用 Shell 实现 ftp 文件的自动上传和下载,封装至 crontab 更可实现定时调度. 1.ftp自动登录批量下载文件 ##### 从ftp服务器上的/home/dat ...

  9. whiledo循环输出数组中的分数

    var scores = [24, 32, 17]; var arrayLength = scores.length; var i =0; while(i < arrayLength){ var ...

  10. oracle查询出来的时间吸附为每5min

    to_char(PACKET_TIME,'yyyy-mm-dd hh24:')||floor(to_char(PACKET_TIME,'mi')/5 )*5||':00' as start_time, ...