RN-入门基础
Props
State
一切界面变化,都是state变化
state修改必须通过setState方法
this.state.like=true 这样复制无效
setState是一个merge合并的操作,只修改指定属性,不影响其他属性
setState是异步操作
样式
通过StyleSheet.create来管理样式
<Text style={styles.bigBlue}>just bigBlue</Text>
<Text style={[styles.bigBlue, styles.red]}>bigBlue, then red</Text>
const styles = StyleSheet.create({
bigBlue: {
color: 'blue',
fontWeight: 'bold',
fontSize: ,
},
red: {
color: 'red',
},
});
高度和宽度
在RN中尺寸是无单位的
<View style={{width: , height: , backgroundColor: 'powderblue'}} />
Flexbox布局
flex:1
flexDirection, justifyContent, alignItems 这三个属性可以满足大部分布局
但是RN中flexDirection的默认属性时column
文本
TextInput允许用户输入文本
onChangeText属性时文本放生变化被调用
<TextInput
style={{height: }}
placeholder="Type here to translate!"
onChangeText={(text) => this.setState({text})}
/>
触摸事件
Button是一个跨平台按钮
点击按钮会调用onPress
Touchable系列组件
TouchableHighlight, TouchableOpacity, TouchableNativeFeedback, TouchableWithoutFeedback
滚动试图
ScrollView,会渲染所有组件,只适合数量不多的滚动元素
<ScrollView>...</ScrollView>
长列表
FlatList
两个重要属性 data/renderItem/keyExtractor
<FlatList
data={movies}
keyExtractor={item => item.id}
renderItem = {this.renderMovies}
/>
renderMovies({item}) {
return (
<View style={styles.container}>
<Image source={{uri: item.posters.thumbnail}} />
<View>
<Text>{item.title}</Text>
<Text>{item.year}</Text>
</View>
</View>
)
}
SectionList
<SectionList
sections={[
{title: 'D', data: ['Devin']},
{title: 'J', data: ['Jackson', 'James', 'Jillian', 'Jimmy', 'Joel', 'John', 'Julie']},
]}
renderItem={({item}) => <Text style={styles.item}>{item}</Text>}
renderSectionHeader={({section}) => <Text style={styles.sectionHeader}>{section.title}</Text>}
keyExtractor={(item, index) => index}
/>
fetch
RN自带网络请求
fetch('https://mywebsite.com/endpoint/', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
firstParam: 'yourValue',
secondParam: 'yourOtherValue',
}),
});
封装成Promise
getRequest(url, method = 'GET'){
return new Promise((resolve, reject) => {
return fetch(url, {
method
})
.then(res => res.json())
.then(resJson => resolve(resJson))
.catch(err => reject(err))
})
}
当然可以使用第三方axios
RN组件
RN内置组件
View/Text/Image/TextInput/ScrollView/StyleSheet
交互控件
Button/Picker/Slider/Switch
列表组件
FlatList/SectionList
特定平台代码
可以使用Platform模块
case1 样式
const styles = StyleSheet.create({
height: Platform.OS === "ios" ? :
});
case2 样式
const styles = StyleSheet.create({
container: {
flex: ,
...Platform.select({
ios: {
backgroundColor: "red"
},
android: {
backgroundColor: "blue"
}
})
}
});
case3 组件
const Component = Platform.select({
ios: () => require("ComponentIOS"),
android: () => require("ComponentAndroid")
})();
<Component />;
特点平台扩展名
项目中创建
BigButton.ios.js
BigButton.android.js
去掉平台扩展名
import BigButton from './BigButton';
检测Android版本
if (Platform.Version === ) {
console.log("Running on Nougat!");
}
检测iOS版本
const majorVersionIOS = parseInt(Platform.Version, );
if (majorVersionIOS <= ) {
console.log("Work around a change in behavior");
}
获取屏幕分辨率
Dimensions.get('window').width
RN-入门基础的更多相关文章
- React Native 入门基础知识总结
中秋在家闲得无事,想着做点啥,后来想想,为啥不学学 react native.在学习 React Native 时, 需要对前端(HTML,CSS,JavaScript)知识有所了解.对于JS,可以看 ...
- mybatis入门基础(二)----原始dao的开发和mapper代理开发
承接上一篇 mybatis入门基础(一) 看过上一篇的朋友,肯定可以看出,里面的MybatisService中存在大量的重复代码,看起来不是很清楚,但第一次那样写,是为了解mybatis的执行步骤,先 ...
- 01shell入门基础
01shell入门基础 为什么学习和使用shell编程 shell是一种脚本语言,脚本语言是相对于编译语言而言的.脚本语言不需要编译,由解释器读取程序并且执行其中的语句,而编译语言需要编译成可执行代码 ...
- Markdown入门基础
// Markdown入门基础 最近准备开始强迫自己写博文,以治疗严重的拖延症,再不治疗就“病入骨髓,司命之所属,无奈何”了啊.正所谓“工欲善其事,必先利其器”,于是乎在写博文前,博主特地研究了下博文 ...
- JavaScript入门基础
JavaScript基本语法 1.运算符 运算符就是完成操作的一系列符号,它有七类: 赋值运算符(=,+=,-=,*=,/=,%=,<<=,>>=,|=,&=).算术运 ...
- C++ STL编程轻松入门基础
C++ STL编程轻松入门基础 1 初识STL:解答一些疑问 1.1 一个最关心的问题:什么是STL 1.2 追根溯源:STL的历史 1.3 千丝万缕的联系 1.4 STL的不同实现版本 2 牛刀小试 ...
- HTML入门基础教程相关知识
HTML入门基础教程 html是什么,什么是html通俗解答: html是hypertext markup language的缩写,即超文本标记语言.html是用于创建可从一个平台移植到另一平台的超文 ...
- Linux shell入门基础(六)
六.Shell脚本编程详解 将上述五部分的内容,串联起来,增加对Shell的了解 01.shell脚本 shell: # #perl #python #php #jsp 不同的脚本执行不同的文本,执行 ...
- Linux shell入门基础(一)
Linux shell入门基础(一): 01.增加删除用户: #useradd byf userdel byf(主目录未删除) userdel -r byf 该用户的属性:usermod 用 ...
- AngularJS入门基础PPT(附下载链接)
学习了Angularjs有段时间,自己写了一个PPT,个人认为总结的非常全面,对于入门基础够了. 大致模块有:Angularjs简单介绍,Angularjs特性,hello world,Control ...
随机推荐
- SDKManager无法更新问题解决办法
用大连东软的镜像代理 配置步骤 启动 Android SDK Manager ,打开主界面,依次选择「Tools」.「Options...」,弹出『Android SDK Manager - Sett ...
- Go语言切片
切片 Go 语言切片相当于是对数组的抽象. 由于Go 数组的长度不可改变,在特定场景中这样的集合就不太适用,Go中提供了一种灵活,功能强悍的内置类型切片("动态数组"),与数组相比 ...
- onkeyup+onafterpaste 只能输入数字和小数点
1.文本框只能输入数字代码(小数点也不能输入)<input onkeyup="this.value=this.value.replace(/\D/g,'')" onafter ...
- 1px和backgroudImage
https://blog.csdn.net/leadn/article/details/78560786 .setTopLine(@c: #C7C7C7) { & { position: re ...
- Spring定义事物通知tx:advice
<aop:config proxy-target-class="false"> <aop:advisor advice-ref="txAdvice ...
- L1-046 整除光棍
这里所谓的“光棍”,并不是指单身汪啦~ 说的是全部由1组成的数字,比如1.11.111.1111等.传说任何一个光棍都能被一个不以5结尾的奇数整除.比如,111111就可以被13整除. 现在,你的程序 ...
- JavaScript线程(第八天)
js是单线程的: js中的线程分为三种 1.页面渲染 2.主代码逻辑 3.事件触发: 下面我们来看一段代码 <script> setTimeout(function(){ conso ...
- web Function函数
javascript中函数定义 js中函数一般定义如下: function functionName(arg0,arg1,arg2,...,argN) { statements;}其中function ...
- Ansible 安装与配置(一)
公司大概有200多云主机需要进行管理,但是如果通过手工管理费时还累最终结果也容易出错,所以考虑通过自动化的方式来管理云主机,目前开源的自动化工具,大家用的比较多的有Ansible和Saltstack这 ...
- Wavelet Ridgelet Curvelet Contourlet Ripplet
Ripplet: A New Transform for Image Processing Jun Xu, Lei Yang and Dapeng Wu Ripplet: A New Transfor ...