1- ##React Native 总结
1+ ## React Native 总结
22
33
44
5- ###Component
6- Component:组件,使用``` extends React.Component `` ` 创建的类为组件。?
7- ``` componentWillMount() ``` 还可以用``` constructor `` ` 来代替:
5+ ### Component
6+ Component:组件,使用` extends React.Component ` 创建的类为组件。?
7+ ` componentWillMount() ` 还可以用` constructor ` 来代替:
88
9+ ``` javascript
910class Label extends React .Component {
1011 constructor (props ) {
1112 super (props);
1213 }
1314 render (){
1415 }
1516}
17+ ```
1618
19+ ### props与state
1720
18- ###props与state
19-
20- #####props属性:
21- 组件可以定义初始值,自己不可更改props属性值,只允许从父组件中传递过来:
21+ ##### props属性:
22+ 组件可以定义初始值,自己不可更改props属性值,只允许从父组件中传递过来:
2223
23- // 父组件
24+ ``` javascript
25+ // 父组件
2426class ParentComponent extends React .Component {
2527 render (){
2628 return (< Child name= " name" > );
2729 }
2830}
2931
30- // 子组件
32+ // 子组件
3133class Child extends React .Component {
3234 render (){
3335 return (< Text > {this .props .name }< / Text > );
3436 }
3537}
38+ ```
3639
37- 父组件向子组件传递name="name"的props属性,在子组件中使用this.props.name引用此属性。
40+ 父组件向子组件传递name="name"的props属性,在子组件中使用this.props.name引用此属性。
3841
39- 属性类型``` prop type ``` 和默认属性 ``` default prop ``` 可以通过类中的 ``` static ``` 来声明:
42+ 属性类型``` prop type ``` 和默认属性 ``` default prop ``` 可以通过类中的 ``` static ``` 来声明:
4043
41- class Demo extends React.Component {
42- // 默认props
44+ ``` javascript
45+ class Demo extends React .Component {
46+ // 默认props
4347 static defaultProps = {
4448 autoPlay: false ,
4549 maxLoops: 10 ,
4650 }
47- // propTypes用于验证转入的props,当向 props 传入无效数据时,JavaScript 控制台会抛出警告
51+ // propTypes用于验证转入的props,当向 props 传入无效数据时,JavaScript 控制台会抛出警告
4852 static propTypes = {
4953 autoPlay: React .PropTypes .bool .isRequired ,
5054 maxLoops: React .PropTypes .number .isRequired ,
@@ -54,60 +58,63 @@ Component
5458 loopsRemaining: this .props .maxLoops ,
5559 }
5660}
57- #####state属性:
58- 组件用来改变自己状态的属性,通常使用``` setState({key:value}) ``` 来改变属性值触发界面刷新,不能使用``` this.state.xxx ``` 来直接改变。
59- 在开发中,一般不会在定时器函数(setInterval、setTimeout等)中来操作state。典型的场景是在接收到服务器返回的新数据,或者在用户输入数据之后。
61+ ```
62+
63+ ##### state属性:
64+ 组件用来改变自己状态的属性,通常使用``` setState({key:value}) ``` 来改变属性值触发界面刷新,不能使用``` this.state.xxx ``` 来直接改变。
65+ 在开发中,一般不会在定时器函数(setInterval、setTimeout等)中来操作state。典型的场景是在接收到服务器返回的新数据,或者在用户输入数据之后。
6066
61- 对于经常改变的数据且需要刷新界面显示,可以使用state。对于不需要改变的属性值可以使用props。React Native建议由顶层的父组件定义state值,并将state值作为子组件的props属性值传递给子组件,这样可以保持单一的数据传递。
67+ 对于经常改变的数据且需要刷新界面显示,可以使用state。对于不需要改变的属性值可以使用props。React Native建议由顶层的父组件定义state值,并将state值作为子组件的props属性值传递给子组件,这样可以保持单一的数据传递。
6268
6369
6470
65- ###生命周期
71+ ###生命周期
6672
67- 我们把组件从``` 装载 ``` ,到``` 渲染 ``` ,再到``` 卸载 ``` 当做一次生命周期,也就是组件的生存状态从``` 装载 ``` 开始到``` 卸载 ``` 为止,期间可以根据属性的变化进行多次渲染。
68- 生命周期的三种状态:
69- - Mounting:装载
73+ 我们把组件从``` 装载 ``` ,到``` 渲染 ``` ,再到``` 卸载 ``` 当做一次生命周期,也就是组件的生存状态从``` 装载 ``` 开始到``` 卸载 ``` 为止,期间可以根据属性的变化进行多次渲染。
74+ 生命周期的三种状态:
75+ - Mounting:装载
70761.componentWillMount()
71772.componentDidMount()
72- - Updating:渲染
78+ - Updating:渲染
73791.componentWillReceiveProps()
74802.shouldComponentUpdate()
75813.componentWillUpdate()
76824.componentDidUpdate()
77- - Unmounting:卸载
83+ - Unmounting:卸载
7884componentWillUnmount()
7985
8086```
81- componentWillMount(),组件开始装载之前调用,在一次生命周期中只会执行一次。
87+ componentWillMount(),组件开始装载之前调用,在一次生命周期中只会执行一次。
8288
83- componentDidMount(),组件完成装载之后立即调用,在一次生命周期中只会执行一次。在这里开始就可以对组件进行各种操作了,比如在组件装载完成后要显示的时候执行动画。
89+ componentDidMount(),组件完成装载之后立即调用,在一次生命周期中只会执行一次。在这里开始就可以对组件进行各种操作了,比如在组件装载完成后要显示的时候执行动画。
8490
8591
86- componentWillUpdate(object nextProps, object nextState),当新的props或者state被接受时,组件属性更新之前调用,这个方法不会被初始渲染调用。不能在这个方法里使用 this.setState()。如果你要响应一个prop变化来更新state,使用componentWillReceiveProps 来替代。
92+ componentWillUpdate(object nextProps, object nextState),当新的props或者state被接受时,组件属性更新之前调用,这个方法不会被初始渲染调用。不能在这个方法里使用 this.setState()。如果你要响应一个prop变化来更新state,使用componentWillReceiveProps 来替代。
8793
88- componentDidUpdate(object prevProps, object prevState),组件属性更新之后调用,每次属性更新都会调用,这个方法不会被初始渲染调用。
94+ componentDidUpdate(object prevProps, object prevState),组件属性更新之后调用,每次属性更新都会调用,这个方法不会被初始渲染调用。
8995
90- componentWillUnmount(),组件卸载之前调用,在这个方法里执行一些必要的清理操作,比如timers。
96+ componentWillUnmount(),组件卸载之前调用,在这个方法里执行一些必要的清理操作,比如timers。
9197```
9298
93- #####组件属性更改时会调用以下方法,在一次生命周期中可以执行多次:
99+ ##### 组件属性更改时会调用以下方法,在一次生命周期中可以执行多次:
94100
95- componentWillReceiveProps(object nextProps),已加载组件收到新的props时被调用.这个方法不会为最初的渲染调用。
101+ componentWillReceiveProps(object nextProps),已加载组件收到新的props时被调用.这个方法不会为最初的渲染调用。
96102
97- shouldComponentUpdate(object nextProps, object nextState),组件判断是否重新渲染时调用,当新的props或者state被收到,在渲染前被调用.这个方法不会在最初的渲染被调用。
103+ shouldComponentUpdate(object nextProps, object nextState),组件判断是否重新渲染时调用,当新的props或者state被收到,在渲染前被调用.这个方法不会在最初的渲染被调用。
98104
99- 并没有类似的 ``` componentWillReceiveState () ``` 的方法。一个即将到来的 prop 转变可能会导致一个 state 变化,但是反之不是。如果你需要实现一个对 state 变化相应的操作,使用 ``` componentWillUpdate() ``` 。
105+ 并没有类似的 ``` componentWillReceiveState () ``` 的方法。一个即将到来的 prop 转变可能会导致一个 state 变化,但是反之不是。如果你需要实现一个对 state 变化相应的操作,使用 ``` componentWillUpdate() ``` 。
100106
101107
102- 如果 shouldComponentUpdate () 返回false, render() 会在下次state变化前被完全跳过。另外componentWillUpdate () 和 componentDidUpdate() 将不会被调用。
108+ 如果 shouldComponentUpdate () 返回false, render() 会在下次state变化前被完全跳过。另外componentWillUpdate () 和 componentDidUpdate() 将不会被调用。
103109
104- 默认情况下shouldComponentUpdate() 总是返回 true 来阻止当 state 突变时的细微bug。
110+ 默认情况下shouldComponentUpdate() 总是返回 true 来阻止当 state 突变时的细微bug。
105111
106112
107- ###页面跳转
113+ ### 页面跳转
108114
109- 初始化第一个页面:
115+ 初始化第一个页面:
110116
117+ ``` javascript
111118import SeatPageComponent from ' ./SeatPageComponent' ;
112119import MainPageComponent from ' ./MainPageComponent' ;
113120import TrainListComponent from ' ./TrainListComponent' ;
@@ -118,129 +125,137 @@ componentWillUnmount()
118125 let defaultComponent = MainPageComponent;
119126 return (
120127 < Navigator
121- // 指定默认页面
128+ // 指定默认页面
122129 initialRoute= {{ name: defaultName, component: defaultComponent }}
123- // 配置页面间跳转动画
130+ // 配置页面间跳转动画
124131 configureScene= {(route ) => {
125132 return Navigator .SceneConfigs .VerticalDownSwipeJump ;
126133 }}
127- // 初始化默认页面
134+ // 初始化默认页面
128135 renderScene= {(route , navigator ) => {
129136 let Component = route .component ;
130- // 将navigator作为props传递到下一个页面
137+ // 将navigator作为props传递到下一个页面
131138 return < Component {... route .params } navigator = {navigator } / >
132139 }} / >
133140 );
134141 }
135142}
136143
144+ ```
137145
138- 跳转到下一页面:
146+ 跳转到下一页面:
139147
148+ ``` javascript
140149jumpToNext (){
141- const { navigator } = this.props;// 由上一个页面传递过来
150+ const { navigator } = this .props ;// 由上一个页面传递过来
142151 if (navigator ) {
143152 navigator .push ({
144153 name: ' SeatPageComponent' ,
145- component: SeatPageComponent,// 下一个页面
154+ component: SeatPageComponent,// 下一个页面
146155 });
147156 }
148157}
158+ ```
149159
150- 返回上一个页面:
160+ 返回上一个页面:
151161
162+ ``` javascript
152163 _back (){
153164 const { navigator } = this .props ;
154165 if (navigator ) {
155166 navigator .pop ();
156167 }
157168 }
169+ ```
158170
159- 页面间通信
171+ 页面间通信
160172
161- 例如:从A页面打开B页面
162- A通过route.params将参数传递给B:
173+ 例如:从A页面打开B页面
174+ A通过route.params将参数传递给B:
163175
176+ ``` javascript
164177jumpToNext (){
165- const { navigator } = this.props;// 由上一个页面传递过来
178+ const { navigator } = this .props ;// 由上一个页面传递过来
166179 if (navigator ) {
167180 navigator .push ({
168181 name: ' SeatPageComponent' ,
169- component: SeatPageComponent,// 下一个页面
170- params: { // 需要传递个下一个页面的参数,第二个页面使用this.props.xxx获取参数
182+ component: SeatPageComponent,// 下一个页面
183+ params: { // 需要传递个下一个页面的参数,第二个页面使用this.props.xxx获取参数
171184 id: 123 ,
172185 title: this .state .title ,
173186 },
174187 });
175188 }
176189}
190+ ```
177191
178- A通过route.params传递回调方法或者A的引用来让B将数据传回给A:
192+ A通过route.params传递回调方法或者A的引用来让B将数据传回给A:
179193
180- // A页面
194+ ``` javascript
195+ // A页面
181196jumpToNext (){
182- const { navigator } = this.props;// 由上一个页面传递过来
197+ const { navigator } = this .props ;// 由上一个页面传递过来
183198 if (navigator ) {
184- let that = this;// this作用域,参见下文函数绑定
199+ let that = this ;// this作用域,参见下文函数绑定
185200 navigator .push ({
186201 name: ' SeatPageComponent' ,
187- component: SeatPageComponent,// 下一个页面
188- params: { // 需要传递个下一个页面的参数,第二个页面使用this.props.xxx获取参数
189- title: '测试',
202+ component: SeatPageComponent,// 下一个页面
203+ params: { // 需要传递个下一个页面的参数,第二个页面使用this.props.xxx获取参数
204+ title: ' 测试' ,
190205 getName : function (name ) {that .setState ({ name: name })}
191206 },
192207 });
193208 }
194209}
195210
196- // B页面
211+ // B页面
197212 _back (){
198213 const { navigator } = this .props ;
199214 if (this .props .getName ){
200- this.props.getName('测试');
215+ this .props .getName (' 测试' );
201216 }
202217 if (navigator ) {
203218 navigator .pop ();
204219 }
205220 }
221+ ```
206222
207223
224+ ### flexbox布局
225+ ##### 什么是flexbox布局
226+ React中引入了flexbox概念,flexbox是属于web前端领域CSS的一种布局方案,是2009年W3C提出了一种新的布局方案,可以简便、完整、响应式地实现各种页面布局。
227+ RN利用flexBox模型布局, 也就是在手机屏幕上对组件进行排列.利用flexBox模型,开发者可以开发出动态宽高的自适应的UI布局。
208228
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. 边框、空隙与填充
229+ ##### flexbox中的样式主要有以下几类:
230+ 1. 位置及宽、高相关的样式键
231+ 2. 容器属性,决定子组件排列规则的键
232+ 3. 元素属性,决定组件显示规则的键
233+ 4. 边框、空隙与填充
219234
220- #####布局样式
235+ #####布局样式
221236- position
222- ``` relative ``` (默认) 表示当前描述的位置是相对定位,不可以使用``` bottom ``` 和``` right ``` 。``` top ``` 和``` left ``` 表示当前组件距离上一个同级组件的最上(左)距离
223- ``` absolute ``` 表示当前描述的位置是绝对定位,``` top ``` 、``` bottom ``` 、``` left ``` 、 ``` right ``` ,描述当前组件的位置距离父组件最(上下、左、右)的距离
237+ ``` relative ``` (默认) 表示当前描述的位置是相对定位,不可以使用``` bottom ``` 和``` right ``` 。``` top ``` 和``` left ``` 表示当前组件距离上一个同级组件的最上(左)距离
238+ ``` absolute ``` 表示当前描述的位置是绝对定位,``` top ``` 、``` bottom ``` 、``` left ``` 、 ``` right ``` ,描述当前组件的位置距离父组件最(上下、左、右)的距离
224239- width
225- ``` width ``` 、``` height ``` 、``` maxHeight ``` 、``` maxWidth ``` 、``` minHeight ``` 、``` minWidth ``` 组件的宽和高是可以动态改变的,所以可以设置宽和高的最大和最小值
240+ ``` width ``` 、``` height ``` 、``` maxHeight ``` 、``` maxWidth ``` 、``` minHeight ``` 、``` minWidth ``` 组件的宽和高是可以动态改变的,所以可以设置宽和高的最大和最小值
226241- flexDirection
227- ``` row ``` :横向排列,<b >主轴</b >为水平方向;
228- ``` ?column ``` :竖直排列,<b >主轴</b >为垂直方向。
242+ ``` row ``` :横向排列,<b >主轴</b >为水平方向;
243+ ``` ?column ``` :竖直排列,<b >主轴</b >为垂直方向。
229244- flexWrap
230- ``` ?wrap ``` ?和 ``` ?nowrap ``` ?(默认值)?,当水平或垂直布局时,如果子View放不下可选 ``` ?wrap ``` ?实现自动换行,
245+ ``` ?wrap ``` ?和 ``` ?nowrap ``` ?(默认值)?,当水平或垂直布局时,如果子View放不下可选 ``` ?wrap ``` ?实现自动换行,
231246- justifyContent
232- 子布局在主轴方向位置 enum(``` flex- start ``` ,``` flex-end ``` ,``` center ``` ,``` space-between ``` ,``` space-around ``` )
247+ 子布局在主轴方向位置 enum(``` flex- start ``` ,``` flex-end ``` ,``` center ``` ,``` space-between ``` ,``` space-around ``` )
233248- alignItems
234- 子布局在侧轴方向位置 enum(``` flex-start ``` ,``` flex-end ``` ,``` center ``` ,``` stretch ``` )
249+ 子布局在侧轴方向位置 enum(``` flex-start ``` ,``` flex-end ``` ,``` center ``` ,``` stretch ``` )
235250- flex
236- 权重,默认值是0当它的值为1时,子组件将自动缩放以适应父组件剩下的空白空间。
251+ 权重,默认值是0当它的值为1时,子组件将自动缩放以适应父组件剩下的空白空间。
237252- alignSelf
238- 忽略它的父组件样式中的``` alignItems ``` 的取值,而对该组件使用``` alignSelf ``` 键对应的规则。
253+ 忽略它的父组件样式中的``` alignItems ``` 的取值,而对该组件使用``` alignSelf ``` 键对应的规则。
239254enum(``` auto ``` ,``` flex-start ``` ,``` flex-end ``` ,``` center ``` ,``` stretch ``` )
240255- padding
241256- margin
242257
243258
244- ![ 盒子模型示意图] ( http://upload-images.jianshu.io/upload_images/1132780-3e6d1a45fb4550ce.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240 )
259+ ![ 盒子模型示意图] ( http://upload-images.jianshu.io/upload_images/1132780-3e6d1a45fb4550ce.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240 )
245260
246261
0 commit comments