一、 JSX 语法

​ 1) 变量要写在 {} 内部;

​ 2)render ( ) 返回的 HTML 需要使用()包起来

二、常用组件

2.1 View

2.2 Text

2.3 Image

1)使用
  • Image 标签负责展示图片

  • 标签内不许再添加子元素

  • 标签必须设置宽高

1
2
3
4
5
6
7
8
9
10
11
// 本地图片
<Image
source={require('../../assets/image/ic-arrow.png')}
style={{ width: 12, height: 21 }}
></Image>

// 图床
<Image
source={{uri:'https://...'}}
style={{ width: 12, height: 21 }}
></Image>
2)图片自适应

可以再宽度固定的情况下动态计算图片的高度(参考

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 componentDidMount() {
let imageObj = Image.resolveAssetSource(
require('../../assets/image/pic-decorate.png')
)
let { width, height } = imageObj
let myHeight = Math.floor((getWidth() / width) * height)
this.setState({
imgObjHg: myHeight,
})
}

<Image
source={require('../../assets/image/pic-decorate.png')}
style={{ width: getWidth(), height: this.state.imgObjHg }}
>
</Image>

2.3 ScrollView

2.4 FlatList

1) 多列布局: numColumns
2)使用实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import React, { Component } from 'react';

import {
Image,
FlatList,
StyleSheet,
Text,
View
} from 'react-native';

const REQUEST_URL =
'https://raw.githubusercontent.com/facebook/react-native/0.51-stable/docs/MoviesExample.json';

export default class SampleAppMovies extends Component {
constructor(props) {
super(props);
this.state = {
data: [],
loaded: false
};
// 在ES6中,如果在自定义的函数里使用了this关键字,则需要对其进行“绑定”操作,否则this的指向会变为空
// 像下面这行代码一样,在constructor中使用bind是其中一种做法(还有一些其他做法,如使用箭头函数等)
this.fetchData = this.fetchData.bind(this);
}

componentDidMount() {
this.fetchData();
}

fetchData() {
fetch(REQUEST_URL)
.then((response) => response.json())
.then((responseData) => {
// 注意,这里使用了this关键字,为了保证this在调用时仍然指向当前组件,我们需要对其进行“绑定”操作
this.setState({
data: this.state.data.concat(responseData.movies),
loaded: true
});
});
}

render() {
if (!this.state.loaded) {
return this.renderLoadingView();
}

return (
<FlatList
data={this.state.data}
renderItem={this.renderMovie}
style={styles.list}
keyExtractor={(item) => item.id}
/>
);
}

renderLoadingView() {
return (
<View style={styles.container}>
<Text>Loading movies...</Text>
</View>
);
}

renderMovie({ item }) {
// { item }是一种“解构”写法,请阅读ES2015语法的相关文档
// item也是FlatList中固定的参数名,请阅读FlatList的相关文档
return (
<View style={styles.container}>
<Image
source={{ uri: item.posters.thumbnail }}
style={styles.thumbnail}
/>
<View style={styles.rightContainer}>
<Text style={styles.title}>{item.title}</Text>
<Text style={styles.year}>{item.year}</Text>
</View>
</View>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF'
},
rightContainer: {
flex: 1
},
title: {
fontSize: 20,
marginBottom: 8,
textAlign: 'center'
},
year: {
textAlign: 'center'
},
thumbnail: {
width: 53,
height: 81
},
list: {
paddingTop: 20,
backgroundColor: '#F5FCFF'
}
});

3) 渲染多个子元素
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
_renderShop() {
return (
<View>
<Text>店铺名字</Text>
{
this.products.map((item,index) => {
<View>
//商品信息
</View>
})
}
</View>
)

}

三、常用事件

四、网络请求

通常在 componentDidMount( ) 中获取请求

五、调试

2.1 chrome 开发者工具

1)使用方式

2)优点

  • 提供 console 输出
  • 可在 source 中断点调试 js 脚本
  • 熟悉 chrome
  1. 缺点
  • 无法展示 App 界面
  • 无法观察到 React Native 中的网络请求
2.2 Safari

1)使用方式

  • 打开调试:Preferences → Advanced → Select “Show Develop menu in menu bar”

  • 选择 Develop → Simulator → JSContext

2.3 React Developer Tools

1)安装

1
yarn add -g react-devtools

2)执行命令

1
react-devtools

参考链接

React Native