Skip to content

Commit a9d3a02

Browse files
committed
d12
1 parent 15a4a00 commit a9d3a02

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

React Native 每日一学/Readme.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
9. [D9:动态属性名&字符串模板(2016-8-30)](#d9动态属性名字符串模板2016-8-30)
1818
10. [D10:优化切换动画卡顿的问题(2016-8-31)](#d10优化切换动画卡顿的问题2016-8-31)
1919
11. [D11:AsyncStorage存储key管理小技巧(2016-9-1)](#d11asyncstorage存储key管理小技巧---)
20+
12. [D12:延展操作符(Spread operator)(2016-9-2)](#d12延展操作符spread-operator2016-9-2)
2021

2122

2223
```
@@ -32,6 +33,42 @@ D1:标题 (日期)
3233
```
3334

3435

36+
D12:延展操作符(Spread operator)(2016-9-2)
37+
------
38+
通常我们在封装一个组件时,会对外公开一些 props 用于实现功能。大部分情况下在外部使用都应显示的传递 props 。但是当传递大量的props时,会非常繁琐,这时我们可以使用 `...(延展操作符,用于取出参数对象的所有可遍历属性)` 来进行传递。
39+
40+
### 一般情况下我们应该这样写
41+
```
42+
<CustomComponent type='normal' number={2} />
43+
```
44+
45+
### 使用 ... ,等同于上面的写法
46+
47+
```
48+
var params = {
49+
type: 'normal',
50+
number: 2
51+
}
52+
<CustomComponent {...params} />
53+
```
54+
55+
### 配合解构赋值避免传入一些不需要的参数
56+
57+
```
58+
var params = {
59+
name: '123',
60+
title: '456',
61+
type: 'aaa'
62+
}
63+
64+
var { type, ...other } = params;
65+
66+
<CustomComponent type='normal' number={2} {...other} />
67+
//等同于
68+
<CustomComponent type='normal' number={2} name='123' title='456' />
69+
```
70+
71+
3572
D11:AsyncStorage存储key管理小技巧
3673
------
3774

0 commit comments

Comments
 (0)