Skip to content

Commit 8de5f6d

Browse files
authored
create
1 parent 4eb2aab commit 8de5f6d

File tree

1 file changed

+246
-0
lines changed

1 file changed

+246
-0
lines changed

react native 基础知识总结

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
##React Native 总结
2+
3+
4+
5+
###Component
6+
Component:组件,使用```extends React.Component```创建的类为组件。

7+
```componentWillMount()```还可以用``` constructor ```来代替:
8+
9+
class Label extends React.Component{
10+
constructor(props) {
11+
super(props);
12+
}
13+
render(){
14+
}
15+
}
16+
17+
18+
###props与state
19+
20+
#####props属性:
21+
组件可以定义初始值,自己不可更改props属性值,只允许从父组件中传递过来:
22+
23+
// 父组件
24+
class ParentComponent extends React.Component{
25+
render(){
26+
return(<Child name="name">);
27+
}
28+
}
29+
30+
// 子组件
31+
class Child extends React.Component{
32+
render(){
33+
return(<Text>{this.props.name}</Text>);
34+
}
35+
}
36+
37+
父组件向子组件传递name="name"的props属性,在子组件中使用this.props.name引用此属性。
38+
39+
属性类型``` prop type ```和默认属性 ```default prop ```可以通过类中的 ```static ```来声明:
40+
41+
class Demo extends React.Component {
42+
// 默认props
43+
static defaultProps = {
44+
autoPlay: false,
45+
maxLoops: 10,
46+
}
47+
// propTypes用于验证转入的props,当向 props 传入无效数据时,JavaScript 控制台会抛出警告
48+
static propTypes = {
49+
autoPlay: React.PropTypes.bool.isRequired,
50+
maxLoops: React.PropTypes.number.isRequired,
51+
posterFrameSrc: React.PropTypes.string.isRequired,
52+
}
53+
state = {
54+
loopsRemaining: this.props.maxLoops,
55+
}
56+
}
57+
#####state属性:
58+
组件用来改变自己状态的属性,通常使用```setState({key:value})```来改变属性值触发界面刷新,不能使用```this.state.xxx```来直接改变。
59+
在开发中,一般不会在定时器函数(setInterval、setTimeout等)中来操作state。典型的场景是在接收到服务器返回的新数据,或者在用户输入数据之后。
60+
61+
对于经常改变的数据且需要刷新界面显示,可以使用state。对于不需要改变的属性值可以使用props。React Native建议由顶层的父组件定义state值,并将state值作为子组件的props属性值传递给子组件,这样可以保持单一的数据传递。
62+
63+
64+
65+
###生命周期
66+
67+
我们把组件从```装载```,到```渲染```,再到```卸载```当做一次生命周期,也就是组件的生存状态从```装载```开始到```卸载```为止,期间可以根据属性的变化进行多次渲染。
68+
生命周期的三种状态:
69+
- Mounting:装载
70+
1.componentWillMount()
71+
2.componentDidMount()
72+
- Updating:渲染
73+
1.componentWillReceiveProps()
74+
2.shouldComponentUpdate()
75+
3.componentWillUpdate()
76+
4.componentDidUpdate()
77+
- Unmounting:卸载
78+
componentWillUnmount()
79+
80+
```
81+
componentWillMount(),组件开始装载之前调用,在一次生命周期中只会执行一次。
82+
83+
componentDidMount(),组件完成装载之后立即调用,在一次生命周期中只会执行一次。在这里开始就可以对组件进行各种操作了,比如在组件装载完成后要显示的时候执行动画。
84+
85+
86+
componentWillUpdate(object nextProps, object nextState),当新的props或者state被接受时,组件属性更新之前调用,这个方法不会被初始渲染调用。不能在这个方法里使用 this.setState()。如果你要响应一个prop变化来更新state,使用componentWillReceiveProps 来替代。
87+
88+
componentDidUpdate(object prevProps, object prevState),组件属性更新之后调用,每次属性更新都会调用,这个方法不会被初始渲染调用。
89+
90+
componentWillUnmount(),组件卸载之前调用,在这个方法里执行一些必要的清理操作,比如timers。
91+
```
92+
93+
#####组件属性更改时会调用以下方法,在一次生命周期中可以执行多次:
94+
95+
componentWillReceiveProps(object nextProps),已加载组件收到新的props时被调用.这个方法不会为最初的渲染调用。
96+
97+
shouldComponentUpdate(object nextProps, object nextState),组件判断是否重新渲染时调用,当新的props或者state被收到,在渲染前被调用.这个方法不会在最初的渲染被调用。
98+
99+
并没有类似的 ```componentWillReceiveState ()```的方法。一个即将到来的 prop 转变可能会导致一个 state 变化,但是反之不是。如果你需要实现一个对 state 变化相应的操作,使用 ```componentWillUpdate()```。
100+
101+
102+
如果 shouldComponentUpdate () 返回false, render() 会在下次state变化前被完全跳过。另外componentWillUpdate () 和 componentDidUpdate() 将不会被调用。
103+
104+
默认情况下shouldComponentUpdate() 总是返回 true 来阻止当 state 突变时的细微bug。
105+
106+
107+
###页面跳转
108+
109+
初始化第一个页面:
110+
111+
import SeatPageComponent from './SeatPageComponent';
112+
import MainPageComponent from './MainPageComponent';
113+
import TrainListComponent from './TrainListComponent';
114+
115+
class MainPage extends React.Component {
116+
render() {
117+
let defaultName = 'MainPageComponent';
118+
let defaultComponent = MainPageComponent;
119+
return (
120+
<Navigator
121+
// 指定默认页面
122+
initialRoute={{ name: defaultName, component: defaultComponent }}
123+
// 配置页面间跳转动画
124+
configureScene={(route) => {
125+
return Navigator.SceneConfigs.VerticalDownSwipeJump;
126+
}}
127+
// 初始化默认页面
128+
renderScene={(route, navigator) => {
129+
let Component = route.component;
130+
// 将navigator作为props传递到下一个页面
131+
return <Component {...route.params} navigator={navigator} />
132+
}} />
133+
);
134+
}
135+
}
136+
137+
138+
跳转到下一页面:
139+
140+
jumpToNext(){
141+
const { navigator } = this.props;// 由上一个页面传递过来
142+
if(navigator) {
143+
navigator.push({
144+
name: 'SeatPageComponent',
145+
component: SeatPageComponent,// 下一个页面
146+
});
147+
}
148+
}
149+
150+
返回上一个页面:
151+
152+
_back(){
153+
const { navigator } = this.props;
154+
if(navigator) {
155+
navigator.pop();
156+
}
157+
}
158+
159+
页面间通信
160+
161+
例如:从A页面打开B页面
162+
A通过route.params将参数传递给B:
163+
164+
jumpToNext(){
165+
const { navigator } = this.props;// 由上一个页面传递过来
166+
if(navigator) {
167+
navigator.push({
168+
name: 'SeatPageComponent',
169+
component: SeatPageComponent,// 下一个页面
170+
params: { // 需要传递个下一个页面的参数,第二个页面使用this.props.xxx获取参数
171+
id: 123,
172+
title: this.state.title,
173+
},
174+
});
175+
}
176+
}
177+
178+
A通过route.params传递回调方法或者A的引用来让B将数据传回给A:
179+
180+
// A页面
181+
jumpToNext(){
182+
const { navigator } = this.props;// 由上一个页面传递过来
183+
if(navigator) {
184+
let that = this;// this作用域,参见下文函数绑定
185+
navigator.push({
186+
name: 'SeatPageComponent',
187+
component: SeatPageComponent,// 下一个页面
188+
params: { // 需要传递个下一个页面的参数,第二个页面使用this.props.xxx获取参数
189+
title: '测试',
190+
getName: function(name) {that.setState({ name: name })}
191+
},
192+
});
193+
}
194+
}
195+
196+
// B页面
197+
_back(){
198+
const { navigator } = this.props;
199+
if(this.props.getName){
200+
this.props.getName('测试');
201+
}
202+
if(navigator) {
203+
navigator.pop();
204+
}
205+
}
206+
207+
208+
209+
###flexbox布局
210+
#####什么是flexbox布局
211+
React中引入了flexbox概念,flexbox是属于web前端领域CSS的一种布局方案,是2009年W3C提出了一种新的布局方案,可以简便、完整、响应式地实现各种页面布局。
212+
RN利用flexBox模型布局, 也就是在手机屏幕上对组件进行排列.利用flexBox模型,开发者可以开发出动态宽高的自适应的UI布局。
213+
214+
#####flexbox中的样式主要有以下几类:
215+
1. 位置及宽、高相关的样式键
216+
2. 容器属性,决定子组件排列规则的键
217+
3. 元素属性,决定组件显示规则的键
218+
4. 边框、空隙与填充
219+
220+
#####布局样式
221+
- position
222+
```relative```(默认) 表示当前描述的位置是相对定位,不可以使用``` bottom```和```right```。```top```和```left```表示当前组件距离上一个同级组件的最上(左)距离
223+
```absolute``` 表示当前描述的位置是绝对定位,``` top``` 、```bottom```、``` left```、 ```right```,描述当前组件的位置距离父组件最(上下、左、右)的距离
224+
- width
225+
```width```、```height```、```maxHeight```、```maxWidth```、```minHeight```、```minWidth``` 组件的宽和高是可以动态改变的,所以可以设置宽和高的最大和最小值
226+
- flexDirection
227+
```row```:横向排列,<b>主轴</b>为水平方向;
228+
```
column```:竖直排列,<b>主轴</b>为垂直方向。
229+
- flexWrap
230+
```
wrap ```
和 ```
nowrap ```
(默认值)
,当水平或垂直布局时,如果子View放不下可选 ```
wrap ```
实现自动换行,
231+
- justifyContent
232+
子布局在主轴方向位置 enum(```flex- start```,```flex-end```,```center```,```space-between```,```space-around```)
233+
- alignItems
234+
子布局在侧轴方向位置 enum(```flex-start```,```flex-end```,```center```,```stretch``` )
235+
- flex
236+
权重,默认值是0当它的值为1时,子组件将自动缩放以适应父组件剩下的空白空间。
237+
- alignSelf
238+
忽略它的父组件样式中的```alignItems```的取值,而对该组件使用```alignSelf```键对应的规则。
239+
enum(```auto```,```flex-start```,```flex-end```,```center```,```stretch```)
240+
- padding
241+
- margin
242+
243+
244+
![盒子模型示意图](http://upload-images.jianshu.io/upload_images/1132780-3e6d1a45fb4550ce.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
245+
246+

0 commit comments

Comments
 (0)