Props

State

  一切界面变化,都是state变化

  state修改必须通过setState方法

    this.state.like=true 这样复制无效

    setState是一个merge合并的操作,只修改指定属性,不影响其他属性

    setState是异步操作

样式

  通过StyleSheet.create来管理样式

  1. <Text style={styles.bigBlue}>just bigBlue</Text>
  2. <Text style={[styles.bigBlue, styles.red]}>bigBlue, then red</Text>
  3.  
  4. const styles = StyleSheet.create({
  5. bigBlue: {
  6. color: 'blue',
  7. fontWeight: 'bold',
  8. fontSize: ,
  9. },
  10. red: {
  11. color: 'red',
  12. },
  13. });

高度和宽度

  在RN中尺寸是无单位的

  1. <View style={{width: , height: , backgroundColor: 'powderblue'}} />

Flexbox布局

  flex:1

  flexDirection, justifyContent, alignItems 这三个属性可以满足大部分布局

  但是RN中flexDirection的默认属性时column

文本

  TextInput允许用户输入文本

  onChangeText属性时文本放生变化被调用

  1. <TextInput
  2.   style={{height: }}
  3.   placeholder="Type here to translate!"
  4.   onChangeText={(text) => this.setState({text})}
    />

触摸事件

  Button是一个跨平台按钮

  点击按钮会调用onPress

  Touchable系列组件

    TouchableHighlight, TouchableOpacity, TouchableNativeFeedback, TouchableWithoutFeedback

滚动试图

  ScrollView,会渲染所有组件,只适合数量不多的滚动元素

  1. <ScrollView>...</ScrollView>

长列表

  FlatList

    两个重要属性 data/renderItem/keyExtractor

  1. <FlatList
  2. data={movies}
  3. keyExtractor={item => item.id}
  4. renderItem = {this.renderMovies}
  5. />
  1. renderMovies({item}) {
  2. return (
  3. <View style={styles.container}>
  4. <Image source={{uri: item.posters.thumbnail}} />
  5. <View>
  6. <Text>{item.title}</Text>
  7. <Text>{item.year}</Text>
  8. </View>
  9. </View>
  10. )
  11. }

  SectionList

  1. <SectionList
  2. sections={[
  3. {title: 'D', data: ['Devin']},
  4. {title: 'J', data: ['Jackson', 'James', 'Jillian', 'Jimmy', 'Joel', 'John', 'Julie']},
  5. ]}
  6. renderItem={({item}) => <Text style={styles.item}>{item}</Text>}
  7. renderSectionHeader={({section}) => <Text style={styles.sectionHeader}>{section.title}</Text>}
  8. keyExtractor={(item, index) => index}
  9. />

fetch

  RN自带网络请求

  1. fetch('https://mywebsite.com/endpoint/', {
  2. method: 'POST',
  3. headers: {
  4. Accept: 'application/json',
  5. 'Content-Type': 'application/json',
  6. },
  7. body: JSON.stringify({
  8. firstParam: 'yourValue',
  9. secondParam: 'yourOtherValue',
  10. }),
  11. });

封装成Promise

  1. getRequest(url, method = 'GET'){
  2. return new Promise((resolve, reject) => {
  3. return fetch(url, {
  4. method
  5. })
  6. .then(res => res.json())
  7. .then(resJson => resolve(resJson))
  8. .catch(err => reject(err))
  9. })
  10. }

当然可以使用第三方axios

RN组件

  RN内置组件

    View/Text/Image/TextInput/ScrollView/StyleSheet

  交互控件

    Button/Picker/Slider/Switch

  列表组件

    FlatList/SectionList

特定平台代码

  可以使用Platform模块

case1 样式

  1. const styles = StyleSheet.create({
  2. height: Platform.OS === "ios" ? :
  3. });

case2 样式

  1. const styles = StyleSheet.create({
  2. container: {
  3. flex: ,
  4. ...Platform.select({
  5. ios: {
  6. backgroundColor: "red"
  7. },
  8. android: {
  9. backgroundColor: "blue"
  10. }
  11. })
  12. }
  13. });

case3 组件

  1. const Component = Platform.select({
  2. ios: () => require("ComponentIOS"),
  3. android: () => require("ComponentAndroid")
  4. })();
  5.  
  6. <Component />;

特点平台扩展名

项目中创建

  1. BigButton.ios.js
  2. BigButton.android.js

去掉平台扩展名

  1. import BigButton from './BigButton';

检测Android版本

  1. if (Platform.Version === ) {
  2. console.log("Running on Nougat!");
  3. }

检测iOS版本

  1. const majorVersionIOS = parseInt(Platform.Version, );
  2. if (majorVersionIOS <= ) {
  3. console.log("Work around a change in behavior");
  4. }

获取屏幕分辨率

  1. Dimensions.get('window').width

RN-入门基础的更多相关文章

  1. React Native 入门基础知识总结

    中秋在家闲得无事,想着做点啥,后来想想,为啥不学学 react native.在学习 React Native 时, 需要对前端(HTML,CSS,JavaScript)知识有所了解.对于JS,可以看 ...

  2. mybatis入门基础(二)----原始dao的开发和mapper代理开发

    承接上一篇 mybatis入门基础(一) 看过上一篇的朋友,肯定可以看出,里面的MybatisService中存在大量的重复代码,看起来不是很清楚,但第一次那样写,是为了解mybatis的执行步骤,先 ...

  3. 01shell入门基础

    01shell入门基础 为什么学习和使用shell编程 shell是一种脚本语言,脚本语言是相对于编译语言而言的.脚本语言不需要编译,由解释器读取程序并且执行其中的语句,而编译语言需要编译成可执行代码 ...

  4. Markdown入门基础

    // Markdown入门基础 最近准备开始强迫自己写博文,以治疗严重的拖延症,再不治疗就“病入骨髓,司命之所属,无奈何”了啊.正所谓“工欲善其事,必先利其器”,于是乎在写博文前,博主特地研究了下博文 ...

  5. JavaScript入门基础

    JavaScript基本语法 1.运算符 运算符就是完成操作的一系列符号,它有七类: 赋值运算符(=,+=,-=,*=,/=,%=,<<=,>>=,|=,&=).算术运 ...

  6. C++ STL编程轻松入门基础

    C++ STL编程轻松入门基础 1 初识STL:解答一些疑问 1.1 一个最关心的问题:什么是STL 1.2 追根溯源:STL的历史 1.3 千丝万缕的联系 1.4 STL的不同实现版本 2 牛刀小试 ...

  7. HTML入门基础教程相关知识

    HTML入门基础教程 html是什么,什么是html通俗解答: html是hypertext markup language的缩写,即超文本标记语言.html是用于创建可从一个平台移植到另一平台的超文 ...

  8. Linux shell入门基础(六)

    六.Shell脚本编程详解 将上述五部分的内容,串联起来,增加对Shell的了解 01.shell脚本 shell: # #perl #python #php #jsp 不同的脚本执行不同的文本,执行 ...

  9. Linux shell入门基础(一)

    Linux shell入门基础(一): 01.增加删除用户: #useradd byf   userdel byf(主目录未删除)  userdel -r byf   该用户的属性:usermod 用 ...

  10. AngularJS入门基础PPT(附下载链接)

    学习了Angularjs有段时间,自己写了一个PPT,个人认为总结的非常全面,对于入门基础够了. 大致模块有:Angularjs简单介绍,Angularjs特性,hello world,Control ...

随机推荐

  1. parallel::ForkManager

    use Parallel::ForkManager; my $MAX_PROCESSES=10;   #申明最大进程数(一次创建的进程越多,越耗内存): my $pm = new Parallel:: ...

  2. sort_gff.py

    import sys infile = sys.argv[1]outfile = sys.argv[2] gff_list = []fh = open(infile)for x in fh:    i ...

  3. 通过for 来获取数组里面的电话

    //存放的是电话号码包含的数字 , , , , , }; //电话号码出现的下标 , , , , , , , , , , }; //方式1 ; i < index.length; i++) { ...

  4. DashBoard创建各种表(一)

    创建透视表 1.首先需要创建一个DashBoard,然后点击开始导航栏中的透视,创建一个透视表,透视表可以交叉式的显示报告,让我们可以更直观的看到多维数据. 2.把CategroyName和Categ ...

  5. WEB测试专题之测试分类

    虽然说是一个功能测试就概括了,但是其实这里面还别有洞天,大概区分为下面几个小类别:WEB测试专题之web测试分类一(1)链接测试链接是Web应用系统的一个主要特征,它是在页面之间切换和指导用户去一些不 ...

  6. MySQL 8.0常见问题

    1.连接问题: 1.1:8.0的驱动地址更换由原来的com.mysql.jdbc.Driver改为com.mysql.cj.jdbc.Driver 1.2:8.0以后访问地址要加上时区.编码等属性jd ...

  7. 随笔:关于Class.getSimpleName()

    最近学习过程中,遇到了Class.getSimpleName()这个方法,就搜索了一些资料: API定义: Class.getName():以String的形式,返回Class对象的"实体& ...

  8. java项目---遍历系统文件(1星)

    package Demo; import java.io.*; public class TraversalContent { public static void main(String []arg ...

  9. CodeForce Div 2 C. Masha and two friends

    题目链接: http://codeforces.com/contest/1080/problem/C 思路:双向延长两个矩形方块的4边,会形成一个被分割为9块的更大立方体. 计算所有的9个方框.方框中 ...

  10. Kettle 部署

    下载kettle包 访问https://community.hitachivantara.com/docs/DOC-1009855下载kettle包 选择想要的版本 下载zip包 解压kettle包 ...