本文基于React Native 0.52

Demo上传到Git了,有需要可以看看,写了新内容会上传的。Git地址 https://github.com/gingerJY/React-Native-Demo

一、总览

  头部通常分为左、中、右三部分,效果图如下:

二、头部组件

  1、创建components文件夹,新建commonHead.js

  2、commonHead.js

头部分为左、中、右三块,我们需要提供接口,获取外部传入的值,从而判断哪一块需要创建。

static propTypes = {
leftItem: PropTypes.func,
titleItem: PropTypes.func,
rightItem: PropTypes.func,
};

  左侧模块(中和右一样)

renderLeftItem(){
if (this.props.leftItem === undefined) return;
return this.props.leftItem();
}

  样式,设置了一些默认样式和布局,也可以通过 navBarStyle 添加样式或是覆盖默认样式

    render() {
return (
<View style={[{width:width,
height:40,
backgroundColor: this.props.navBarColor || '#fff',//背景色,默认白色
flexDirection:'row',//横向排
justifyContent:'space-between',//主轴对齐方式
alignItems: 'center',//次轴对齐方式(上下居中)
borderBottomWidth: this.props.borderBottomWidth || 0,//是否有下边框
borderColor: this.props.borderColor || '#ccc',
}, this.props.navBarStyle,]}> <View>
{this.renderLeftItem()}
</View>
<View>
{this.renderTitleItem()}
</View>
<View>
{this.renderRightItem()}
</View>
</View>
);
}

commonHead.js完整代码 https://github.com/gingerJY/example/blob/master/RN_commonHead/commonHead.js

三、使用组件

  1、在home.js中引入头部组件

import CommonHead from '../../components/commonHead';

  2、写左中右三部分

    // 头部左侧
renderLeftItem() {
return (
<TouchableOpacity onPress={() => { this.props.navigation.navigate('Search') }} style={styles.navLeft}>
<Image source={require('../../img/scanning.png')} style={styles.navIcon} />
<Text style={styles.navText}>扫一扫</Text>
</TouchableOpacity>
)
}
// 头部中间
renderTitleItem() {
return (
<TouchableOpacity onPress={() => { this.props.navigation.navigate('Search') }}>
<View style={styles.searchBox}>
<Image source={require('../../img/search.png')} style={styles.searchIcon} />
<Text style={styles.searchContent}>搜索商品, 共10161款好物</Text>
</View>
</TouchableOpacity>
)
}
// 头部右侧
renderRightItem() {
return (
<TouchableOpacity onPress={() => { this.props.navigation.navigate('MessageCenter') }} style={styles.navRight}>
<Image source={require('../../img/remind.png')} style={styles.navIcon} />
<Text style={styles.navText}>消息</Text>
</TouchableOpacity>
)
}

  3、使用commonHead

<CommonHead
leftItem={() => this.renderLeftItem()}
titleItem={() => this.renderTitleItem()}
rightItem={() => this.renderRightItem()}
/>

  home.js完整代码 https://github.com/gingerJY/example/blob/master/RN_commonHead/home.js

注:上面的代码有些内容,如样式等没有写到,点链接可以看到完整代码,文章开头那个git地址是整个项目的代码。

另:图标来自 http://www.iconfont.cn/

END---------------------------------------------------------------  

上有天堂,下有书房。

React Native学习(四)—— 写一个公用组件(头部)的更多相关文章

  1. 深入浅出React Native 3: 从零开始写一个Hello World

    这是深入浅出React Native的第三篇文章. 1. 环境配置 2. 我的第一个应用 将index.ios.js中的代码全部删掉,为什么要删掉呢?因为我们准备从零开始写一个应用~学习技术最好的方式 ...

  2. React Native学习-调取摄像头第三方组件:react-native-image-picker

    近期做的软件中图片处理是重点,那么自然也就用到了相机照相或者相册选取照片的功能. react-native中有image-picker这个第三方组件,但是0.18.10这个版本还不是太支持iPad. ...

  3. iOS、swift、React Native学习常用的社区、论坛

    <!----iOS> <!----Swift>*IOS开发常用社区:http://code4app.com/ *IOS开发常用社区:http://www.cocoachina. ...

  4. React Native 学习笔记--进阶(二)--动画

    React Native 进阶(二)–动画 动画 流畅.有意义的动画对于移动应用用户体验来说是非常必要的.我们可以联合使用两个互补的系统:用于全局的布局动画LayoutAnimation,和用于创建更 ...

  5. react native 学习一(环境搭配和常见错误的解决)

    react native 学习一(环境搭配) 首页,按照http://reactnative.cn/docs/0.30/getting-started.html#content上的介绍,下载安装pyt ...

  6. React Native 学习-01

    React Native 学习 (学习版本 0.39) 一.环境配置 二.IDE选择 webstorm 1.webstorm配置 ①.首先是可以选择使用汉化包汉化.eu68 ②.安装插件和外部库. 由 ...

  7. 基于React Native的Material Design风格的组件库 MRN

    基于React Native的Material Design风格的组件库.(为了平台统一体验,目前只打算支持安卓) 官方网站 http://mrn.js.org/ Github https://git ...

  8. 写一个vue组件

    写一个vue组件 我下面写的是以.vue结尾的单文件组件的写法,是基于webpack构建的项目.如果还不知道怎么用webpack构建一个vue的工程的,可以移步到vue-cli. 一个完整的vue组件 ...

  9. React Native 学习资料

    React Native 学习资料 学习资料 网址 React Native中文网 https://reactnative.cn/

随机推荐

  1. Wincc flexable的IO域组态

    1.题目 2.新建三个变量 3.组态画面,添加IO域1 1)常规设置 2)属性设置 4.组态IO域2 1)常规项 2)属性设置 5.组态第三个IO域 1)常规设置 2)属性设置 6.此外可以设置动画 ...

  2. 507. Perfect Number

    We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...

  3. ubuntu下加载mcypt

    mcrypt 是php 里面重要的加密支持扩展库,linux环境下:该库在默认情况下不开启.window环境下:PHP>=5.3,默认开启mcrypt扩展 1.命令行下载(不嫌麻烦可以到网上找安 ...

  4. selenium爬取百度图片

    一:简介 通过selenium模块,模拟火狐浏览器进行搜索下载操作. 二:脚本内容 # -*- coding:utf-8 -*- # 百度图片自动爬去 # Chrome浏览器类似,设置其options ...

  5. String 转化成java.sql.Date和java.sql.Time

    String类型转换成java.sql.Date类型不能直接进行转换,首先要将String转换成java.util.Date,在转化成java.sql.Date 请点击--->   java架构 ...

  6. MySQL5.6中date和string的转换和比较

    Conversion & Comparison, involving strings and dates in MySQL 5.6 我们有张表,表中有一个字段dpt_date,SQL类型为da ...

  7. Codebase Refactoring (with help from Go)

    Codebase Refactoring (with help from Go) 代码库重构(借助于Go) 1.摘要 Go应该添加为类型创建替代等效名称的能力,以便在代码库重构期间渐进代码修复.本文解 ...

  8. Linux(CentOS6.5)下创建新用户和组,并制定用户和组ID

    相关命令: groupadd -g 888 comexgroup useradd comex -d /comexHome -g comexgroup -u 888 cp /etc/skel/.* /c ...

  9. Webpack 2 视频教程 006 - 使用快捷方式进行编译

    原文发表于我的技术博客 这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲 ...

  10. jQuery中事件对象e的事件冒泡用法示例(事件冒泡与阻止冒泡)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...