React-Native进阶_1.抽取样式和组件
组织应用的样式和组件 就像抽取工具类一样,放在单独的文件中,在要使用的地方去导入调用即可。
1.导出样式
Style 样式可以单独写在一个JavaScript文件中,然后导出给其他JavaScript文件使用
比如创建创建Main.js 文件,里面导出styles
import {
StyleSheet,
} from 'react-native';
const styles = StyleSheet.create({
item:{
flexDirection:'row',
borderBottomWidth:1,
borderColor:'rgba(100,53,201,0.1)',
paddingBottom:6,
marginBottom:6,
flex:1,
},
itemContent:{
flex:1,
marginLeft:13,
marginTop:6,
},
itemHeader:{
fontSize:18,
fontFamily:'Helvetica Neue',
fontWeight:'300',
color:'#6435c9',
marginBottom:6,
},
itemMeta:{
fontSize:16,
color:'rgba(0,0,0,0.6)',
marginBottom:6,
},
redText:{
color:'#db2828',
},
listView:{
paddingTop: 20,
backgroundColor: '#F5FCFF',
},
loading:{
flex:1,
justifyContent:'center',
alignItems:'center',
},
overlay: {
backgroundColor: 'rgba(0,0,0,0.3)',
alignItems: 'center'
},
overlayHeader: {
fontSize: 33,
fontFamily: 'Helvetica Neue',
fontWeight: '200',
color: '#eae7ff',
padding: 10
},
overlaySubHeader: {
fontSize: 16,
fontFamily: 'Helvetica Neue',
fontWeight: '200',
color: '#eae7ff',
padding: 10,
paddingTop: 0,
},
backImage: {
// alignItems:'center',
flex: 1,
//justifyContent:'center',
resizeMode: 'cover',
},
image: {
height: 150,
width: 100,
justifyContent: 'center',
},
itemOne: {
// alignSelf:'flex-start',
},
itemTwo: {
//alignSelf:'center',
},
itemThree: {
flex: 2,
},
itemText: {
fontSize: 33,
fontFamily: 'Helvetica Neue',
fontWeight: '200',
color: '#6435c9',
padding: 30,
},
Container: {
//alignItems:'flex-start',// flex-start 靠在左边显示 center 居中 flex-end 尾部
//
flexDirection: 'column',//row column 方向
backgroundColor: '#eae7ff',
flex: 1,
//justifyContent:'center',//center ; 居中 flex-end 位于底部 space-between/space-around屏幕平均分配
},
Text: {
color: '#6435c9',
fontSize: 26,
textAlign: 'center',
fontStyle: 'italic',
letterSpacing: 2,
lineHeight: 33,
fontFamily: 'Helvetica Neue',
fontWeight: '300',
textDecorationLine: 'underline',
textDecorationStyle: 'dashed',
},
});
export {styles as default};
然后在其他JavaScript 文件中通过
import styles from './app/Styles/Main'; 导入声明的样式,然后直接使用styles
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
'use strict'
import React, {Component} from 'react';
import styles from './app/Styles/Main';
import {
AppRegistry,
Text,
Image,
View,
ListView,
} from 'react-native';
//import {AppRegistry,} from 'react-native';
//import Day0718 from './componets/Home'
let REQUEST_URL = 'https://api.douban.com/v2/movie/top250';
export default class Day0718 extends Component {
constructor(props) {
super(props);
this.state = {
dataSource:new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}),
loaded: false,
};
}
componentDidMount(){
this._fetchData();
}
_fetchData(){
fetch(REQUEST_URL)
.then(response => response.json())
.then(responseData =>{
this.setState({
movies:this.state.dataSource.cloneWithRows(responseData.subjects),
loaded: true,
});
})
.done();
}
render() {
if(!this.state.loaded){
return this._renderLoadingView();
}
return (
<View style={styles.Container}>
<ListView
dataSource={this.state.movies}
renderRow={this._renderMovieList}
style={styles.listView}
/>
</View>
);
}
_renderMovieList(movie){
return(
<View style = {styles.item}>
<View style = {styles.itemImage}>
<Image
style ={styles.image}
source ={{uri:movie.images.large}}/>
</View>
<View style = {styles.itemContent}>
<Text style ={styles.itemHeader}>{movie.title}</Text>
<Text style ={styles.itemMeta}>{movie.original_title}({movie.year})</Text>
<Text style ={styles.redText}>{movie.rating.average}</Text>
</View>
</View>
);
};
_renderLoadingView(){
return (
<View style ={styles.loading}>
<Text > loading movies.....</Text>
</View>
);
};
}
class ComText extends React.Component {
render() {
return (
<Text style={styles.itemText}>
{this.props.children}
</Text>
);
}
}
AppRegistry.registerComponent('Day0718', () => Day0718);
2.导出组件
MovieList.js
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
'use strict'
import React, {Component} from 'react';
import styles from '../Styles/Main';
import {
Text,
Image,
View,
ListView,
} from 'react-native';
let REQUEST_URL = 'https://api.douban.com/v2/movie/top250';
export default class Day0718 extends Component {
constructor(props) {
super(props);
this.state = {
dataSource:new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}),
loaded: false,
};
}
componentDidMount(){
this._fetchData();
}
_fetchData(){
fetch(REQUEST_URL)
.then(response => response.json())
.then(responseData =>{
this.setState({
movies:this.state.dataSource.cloneWithRows(responseData.subjects),
loaded: true,
});
})
.done();
}
render() {
if(!this.state.loaded){
return this._renderLoadingView();
}
return (
<View style={styles.Container}>
<ListView
dataSource={this.state.movies}
renderRow={this._renderMovieList}
style={styles.listView}
/>
</View>
);
}
_renderMovieList(movie){
return(
<View style = {styles.item}>
<View style = {styles.itemImage}>
<Image
style ={styles.image}
source ={{uri:movie.images.large}}/>
</View>
<View style = {styles.itemContent}>
<Text style ={styles.itemHeader}>{movie.title}</Text>
<Text style ={styles.itemMeta}>{movie.original_title}({movie.year})</Text>
<Text style ={styles.redText}>{movie.rating.average}</Text>
</View>
</View>
);
};
_renderLoadingView(){
return (
<View style ={styles.loading}>
<Text > loading movies.....</Text>
</View>
);
};
}
在要使用的JavaScript 文件中 导入使用
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
'use strict'
import React, {Component} from 'react';
import MovieList from './app/Components/MovieList';
import {
AppRegistry,
} from 'react-native';
class Day0718 extends Component {
constructor(props) {
super(props);
}
render() {
return( <MovieList />);
}
}
AppRegistry.registerComponent('Day0718', () => Day0718);
-------------------起分享,一起进步!需要你们
-----------------------欢迎各位对React-Native感兴趣的小伙伴加群
React-Native进阶_1.抽取样式和组件的更多相关文章
- 基于React Native的Material Design风格的组件库 MRN
基于React Native的Material Design风格的组件库.(为了平台统一体验,目前只打算支持安卓) 官方网站 http://mrn.js.org/ Github https://git ...
- [RN] React Native 好用的时间线 组件
React Native 好用的时间线 组件 效果如下: 实现方法: 一.组件封装 CustomTimeLine.js "use strict"; import React, {C ...
- react native进阶
一.前沿||潜心修心,学无止尽.生活如此,coding亦然.本人鸟窝,一只正在求职的鸟.联系我可以直接微信:jkxx123321 二.项目总结 **||**文章参考资料:1. http://blog ...
- [React Native]高度自增长的TextInput组件
之前我们学习了从零学React Native之11 TextInput了解了TextInput相关的属性. 在开发中,我们有时候有这样的需求, 希望输入区域的高度随着输入内容的长度而增长, 如下: 这 ...
- React Native填坑之旅--Stateless组件
Stateless component也叫无状态组件.有三种方法可以创建无状态组件. 坑 一般一个组件是怎么定义的: 很久以前的方法: const Heading = createClass({ re ...
- React Native 之轮播图swiper组件
注释:swiper组件是第三方组件 所以在使用之前应该先在命令行安装,然后将第三方的模块引入(第三方模块地址:https://github.com/leecade/react-native-swipe ...
- React Native学习-调取摄像头第三方组件:react-native-image-picker
近期做的软件中图片处理是重点,那么自然也就用到了相机照相或者相册选取照片的功能. react-native中有image-picker这个第三方组件,但是0.18.10这个版本还不是太支持iPad. ...
- react native 传值方式之 :子组件通过调用 其父组件传来的方法 传值回其父组件
- React Native 学习笔记--进阶(二)--动画
React Native 进阶(二)–动画 动画 流畅.有意义的动画对于移动应用用户体验来说是非常必要的.我们可以联合使用两个互补的系统:用于全局的布局动画LayoutAnimation,和用于创建更 ...
随机推荐
- MysQL使用一高级应用(上)
简介 实体与实体之间有3种对应关系,这些关系也需要存储下来 在开发中需要对存储的数据进行一些处理,用到内置的一些函数 视图用于完成查询语句的封装 事务可以保证复杂的增删改操作有效 关系 创建成绩表sc ...
- MySQL MERGE存储引擎 简介及用法
MERGE存储引擎把一组MyISAM数据表当做一个逻辑单元来对待,让我们可以同时对他们进行查询.构成一个MERGE数据表结构的各成员MyISAM数据表必须具有完全一样的结构.每一个成员数据表的数据列必 ...
- Tomcat access log配置(二)
前次讨论了spring boot 中添加Tomcat access log 是轻松愉快,配置文件中添加server.tomcat.accesslog即可,那么如果是外置的Tomcat容器又该如何配置呢 ...
- NOIP2015 T4 推销员 贪心+堆优化
前几天在学堆,这个数据结构貌似挺简单的,但是我看了很久啊QAQ... 今天算是搞懂了吧...于是想到了这道题...(当初悄悄咪咪看题解记得一点) 点我看题 放洛谷的题... 题意的话,大概就是有n个房 ...
- 在Linux Centos 7.2 上安装指定版本Docker。
相关资料链接: https://docs.docker.com/install/linux/docker-ce/centos/#install-docker-ce 先清空下“历史” yum remov ...
- u-boot-2015.07 make xxx_config 分析
1.u-boot编译脚本:mk.sh #! /bin/sh export PATH=$PATH:/opt/ti-sdk-am335x-evm-08.00.00.00/linux-devkit/sysr ...
- 关于javascript以及jquery如何打开文件
其实很简单, <input type="file" id="file" mce_style="display:none"> 这个 ...
- Linux安装jdk、删除Open jdk
1.将jdk解压安装完成后,在bin目录下查看当前jdk的版本号 命令: ./java -version 2.编辑修改配置 1. 修改profile文件 进入命令: vi /etc/profil ...
- IE类兼容一
X-UA-Compatible是自从IE8新加的一个设置,对于IE8以下的浏览器是不识别的.通过在meta中设置X-UA-Compatible的值,可以指定网页的兼容性模式设置. 在网页中指定的模式优 ...
- BinLog日志
一.概述 binlog 二进制日志文件,可以说是MySQL最重要的日志了,它记录了所有的DDL和DML(除了数据查询语句)语句,以事件形式记录,还包含语句所执行的消耗的时间,MySQL的二进制日志是事 ...