原文地址:http://reactnative.cn/docs/0.31/props.html#content

1. property:

如下代码所示

import React, { Component } from 'react';
import { AppRegistry, Image } from 'react-native'; class Bananas extends Component {
render() {
let pic = {
uri: 'https://upload.wikimedia.org/wikipedia/commons/d/de/Bananavarieties.jpg'
};
return (
<Image source={pic} style={{width: 193, height: 110}} />
);
}
} AppRegistry.registerComponent('Bananas', () => Bananas); 上面 <>中间包裹的对象,source={ xxx } 其中source就是图片的属性,代表它的源地址. 类似于HTML中src="http://..."
后面{}中间标示的是属性的value(值)。通过后面style={} 属性推测,里面{}中间的value因该是存在属性变量或者对象。
{{width: 193, height: 110}} 里面有点类似于一个json数据,也可以理解为一个表达式。 因此我们可以把任意合法的JavaScript表达式通过括号嵌入到JSX语句中。

我们自定义组件也可以使用props(property),通过在不同的场景使用不同的属性定制,可以尽量提高自定义组件的复用范畴,只需在render函数中引用this.props,然后按需处理。 举个栗子:
import React,{Component }from ‘react’;
import {AppRegister,Text,View} form 'react-native';
class Greeting extends Component {
render() (
return (<text>hello {this.props.name}!</text>
);
}
}
 class LotsOfGreetings extends Component {
render() {
return(
<View style={{alignItems:'center'}}>
<Greeting name = 'Rexxar'/>
<Greeting name = 'Jaina'/>
<Greeting name = 'Valeera'/>
);
}
} AppRegistry.registerComponent('LotsOfGreetings',()=>
LotsOfGreetings);

2. State:
如果需要在constuctor中国年初始化state,需要在修改时调用setState方法。
示例,闪烁文字效果。
文字内容可以在组件创建时指定,所以对应文字的隐藏和闪烁状态是随着时间变化的。
import React,{ component } from 'react';
import {AppRegistry,Text,View} from 'react-native';
class Blink extends Component {
constructor(props) {
super(props);
this.state = { showText: true }; // 每1000毫秒对showText状态做一次取反操作
setInterval(() => {
this.setState({ showText: !this.state.showText });
}, 1000);
} render() {
// 根据当前showText的值决定是否显示text内容
let display = this.state.showText ? this.props.text : ' ';
return (
<Text>{display}</Text>
);
}
}
class BlinkApp extends Component {
render() {
return (
<View>
<Blink text='I love to blink' />
<Blink text='Yes blinking is so great' />
<Blink text='Why did they ever take this out of HTML' />
<Blink text='Look at me look at me look at me' />
</View>
);
}
} AppRegistry.registerComponent('BlinkApp', () => BlinkApp);
3. 样式
后设置的样式会比先设置的样式优先级高
import React, { Component } from 'react';
import { AppRegistry, StyleSheet, Text, View } from 'react-native'; class LotsOfStyles extends Component {
render() {
return (
<View>
<Text style={styles.red}>just red</Text>
<Text style={styles.bigblue}>just bigblue</Text>
<Text style={[styles.bigblue, styles.red]}>bigblue, then red</Text>
<Text style={[styles.red, styles.bigblue]}>red, then bigblue</Text>
</View>
);
}
} const styles = StyleSheet.create({
bigblue: {
color: 'blue',
fontWeight: 'bold',
fontSize: 30,
},
red: {
color: 'red',
},
}); AppRegistry.registerComponent('LotsOfStyles', () => LotsOfStyles);

常见的做法是按顺序声明和使用style属性,以借鉴CSS中的“层叠”做法(即后声明的属性会覆盖先声明的同名属性)。

4. 宽度与高度

可以将其定义为style属性,在{}中写成json格式字符串。 可以通过点语法,定义内连样式。

import React, { Component } from 'react';
import { AppRegistry, View } from 'react-native'; class FixedDimensionsBasics extends Component {
render() {
return (
<View>
<View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} />
<View style={{width: 100, height: 100, backgroundColor: 'skyblue'}} />
<View style={{width: 150, height: 150, backgroundColor: 'steelblue'}} />
</View>
);
}
};
// 注册应用(registerComponent)后才能正确渲染
// 注意:只把应用作为一个整体注册一次,而不是每个组件/模块都注册
AppRegistry.registerComponent('AwesomeProject', () => FixedDimensionsBasics); 5. 弹性样式
flex=1,组件占据整个剩余空间,如果有多个组件,name这些组件平分剩余空间。
import React, { Component } from 'react';
import { AppRegistry, View } from 'react-native'; class FlexDimensionsBasics extends Component {
render() {
return (
// 试试去掉父View中的`flex: 1`。
// 则父View不再具有尺寸,因此子组件也无法再撑开。
// 然后再用`height: 300`来代替父View的`flex: 1`试试看?
<View style={{flex: 1}}>
<View style={{flex: 1, backgroundColor: 'powderblue'}} />
<View style={{flex: 2, backgroundColor: 'skyblue'}} />
<View style={{flex: 3, backgroundColor: 'steelblue'}} />
</View>
);
}
}; AppRegistry.registerComponent('AwesomeProject', () => FlexDimensionsBasics); 6. 使用Flexbox进行布局:

Flexbox可以在不同屏幕尺寸上提供一致的布局结构。

一般来说,使用flexDirectionalignItems和 justifyContent三个样式属性就已经能满足大多数布局需求。

React Native中的Flexbox的工作原理和web上的CSS基本一致,当然也存在少许差异。首先是默认值不同:flexDirection的默认值是column而不是rowalignItems的默认值是stretch而不是flex-start,以及flex只能指定一个数字值。

在组件的style中指定flexDirection可以决定布局的主轴。子元素是应该沿着水平轴(row)方向排列,还是沿着竖直轴(column)方向排列呢?默认值是竖直轴(column)方向。  <View style={{flex: 1, flexDirection: 'row'}}>

justifyContent可以决定其子元素沿着主轴的排列方式, flex-start,center,flex-end,space-around以及space-bettween.

在组件的style中指定alignItems可以决定其子元素沿着次轴(与主轴垂直的轴,比如若主轴方向为row,则次轴方向为column)的排列方式。子元素是应该靠近次轴的起始端还是末尾段分布呢?亦或应该均匀分布?对应的这些可选项有:flex-startcenterflex-end以及stretch

2.ReactNative Properties|States|Styles 笔记的更多相关文章

  1. java.util.Properties类 学习笔记

    学习目标:   1.认识properties文件,理解其含义,会正确创建properties文件. 2.会使用java.util.Properties类来操作properties文件. 3.掌握相对路 ...

  2. Properties类学习笔记

    1.Properties是一个hashTable子类,但它只装String类型的值2.一个Properties集中有一个子Properties集为它的默认属性集,如果在Properties中找不到相关 ...

  3. 【转】Android开发笔记(序)写在前面的目录

    原文:http://blog.csdn.net/aqi00/article/details/50012511 知识点分类 一方面写写自己走过的弯路掉进去的坑,避免以后再犯:另一方面希望通过分享自己的经 ...

  4. http://lib.csdn.net/article/reactnative/40118

    http://lib.csdn.net/article/reactnative/40118 ES6学习笔记(四)--数值与数组 作者:SirM2z 数值的扩展 Number.isFinite(), N ...

  5. freemarker遍历java.util.Properties

    java.util.Properties类 学习笔记 http://trans.blog.51cto.com/503170/110227/ FreeMarker代码 <#list systemP ...

  6. Styles and Themens(1)详述

    Styles and Themes IN THIS DOCUMENT Defining Styles Inheritance Style Properties Applying Styles and ...

  7. Smashing The Browser:From Vulnerability Discovery To Exploit学习记录

    浏览器Fuzz技术 漏洞挖掘 白盒挖掘 代码审计 自动化代码分析 黑盒挖掘 Fuzzing 两种Fuzzing技术 静态Fuzzing 基于变异的 文件.文档 多媒体 bf3 基于生成的 浏览器 重点 ...

  8. React Native初探

    前言 很久之前就想研究React Native了,但是一直没有落地的机会,我一直认为一个技术要有落地的场景才有研究的意义,刚好最近迎来了新的APP,在可控的范围内,我们可以在上面做任何想做的事情. P ...

  9. cmd for 循环拷贝文件

    这几天忙活部署测试环境, 中途需要拷贝 文件, 直接贴code吧: ::/定义原路径 set source=seventrat_test_backend,seventrat_test_frontend ...

随机推荐

  1. 关于JS嵌套点击事件的问题。

    $().click() 是点击命令$().click(function(){代码}) 是绑定click事件,并不会直接运行.所以在嵌套的时候就有可能出现重复绑定的问题.下面是使用jsonp跨站访问代码 ...

  2. R扩展包

    log10() .libPaths()#查看R包目录 library()#查看以前安装的函数 search() #安装R包的方式 install.packages("car")#安 ...

  3. WPF绑定xml数据源

    1.界面 <UserControl x:Class="HKDCMS.Client.Demo.UIViews.UIControls.AboutUsControl" xmlns= ...

  4. javascript中apply、call和bind的区别

    在JS中,这三者都是用来改变函数的this对象的指向的,他们有什么样的区别呢.在说区别之前还是先总结一下三者的相似之处:1.都是用来改变函数的this对象的指向的.2.第一个参数都是this要指向的对 ...

  5. Android 自定义view (一)——attr 理解

    前言: 自定义view是android自定义控件的核心之一,那么在学习自定义view之前,我们先来了解下自定义view的自定义属性的attr的用法吧 Android attr 是什么 (1)attr ...

  6. Super Jumping! Jumping! Jumping!

    Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. May ...

  7. Slider滑动条

    Slider的Value Changed事件一般与Label结合让其显示数值 int mySlider = (int)sender.value; self.sliderLabel.text = [NS ...

  8. 浅谈在静态页面上使用动态参数,会造成spider多次和重复抓取的解决方案

    原因: 早期由于搜索引擎蜘蛛的不完善,蜘蛛在爬行动态的url的时候很容易由于网站程序的不合理等原因造成蜘蛛迷路死循环. 所以蜘蛛为了避免之前现象就不读取动态的url,特别是带?的url 解决方案: 1 ...

  9. [Struts2] Action Implements SessionAware

    struts2 的Action中若希望访问Session对象,可采用两种方式: 1.从ActionContext中获取: 2.实现SessionAware接口. 1.从ActionContext中获取 ...

  10. 移植mbed到目标板

    上一篇我们导出了mbed基本环境到mdk,根据实际目标还需要做些修改.手头的硬件是ebox平台,芯片STM32F103C8T6,调试器jlink,默认使用UART1. 导出时所选择的NUCLEO-F1 ...