React Native-5.React Native组件封装,组件传值实例开发
发布时间:2020-12-15 04:41:09 所属栏目:百科 来源:网络整理
导读:接上一篇,我们来练习一下组件的封装和组件的传值 九宫格例子: 老样子,我们又图,没图说个xx 预期效果: 先来看看没有封装组件前的代码 'use strict' ; var React = require ( 'react-native' ); var { AppRegistry,StyleSheet,Text,View,PixelRatio,} = R
九宫格例子:老样子,我们又图,没图说个xx 先来看看没有封装组件前的代码'use strict';
var React = require('react-native');
var {
AppRegistry,StyleSheet,Text,View,PixelRatio,} = React;
var stylesForXC = StyleSheet.create({
container : {
height: 84,borderWidth:1,borderColor: 'red',flexDirection: 'row',marginTop: 25,marginLeft: 5,marginRight: 5,borderRadius: 5,/*圆角半径*/
padding: 2,backgroundColor: '#ff0997',},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(),}
})
var wxsPrj = 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.lineCenter]}> <Text style= {stylesForXC.font}>美女</Text> </View> <View style = {[stylesForXC.center,stylesForXC.flex]}> <Text style= {stylesForXC.font}>年货来了</Text> </View> </View> </View> </View> ); } }) AppRegistry.registerComponent('wxsPrj',() => wxsPrj);
我们发现,在主接口函数中的界面布局很多,这样不利于我们模块化的思想我们搞出些事情,把里边界面的布局代码封装成一个组件
|