File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 10102 . [ D2: React Native import 文件的小技巧 (2016-8-19)] ( #d2react-native-import-文件的小技巧-2016-8-19 )
11113 . [ D3: React Native 真机调试 (2016-8-22)] ( #d3react-native-真机调试-2016-8-22 )
12124 . [ D4: React Native 函数的绑定 (2016-8-23)] ( #d4react-native-函数的绑定-2016-8-23 )
13+ 5 . [ D5: React Native setNativeProps使用 (2016-8-24)] ( #d5react-native-setnativeprops使用2016-8-24 )
1314
1415```
1516模板:
@@ -22,6 +23,40 @@ D1:标题 (日期)
2223内容
2324另外:记得在列表中添加链接
2425```
26+
27+
28+
29+ D5: React Native setNativeProps使用(2016-8-24)
30+ ----
31+
32+ 有时候我们需要直接改动组件并触发局部的刷新,但不使用` state ` 或是` props ` 。
33+ ` setNativeProps ` 方法可以理解为web的直接修改dom。使用该方法修改 ` View ` 、 ` Text ` 等 RN自带的组件 ,则不会触发组件的 ` componentWillReceiveProps ` 、 ` shouldComponentUpdate ` 、` componentWillUpdate ` 等组件生命周期中的方法。
34+
35+ ### 使用例子
36+
37+ ``` javascript
38+ class MyButton extends React .Component ({
39+ setNativeProps (nativeProps ) {
40+ this ._root .setNativeProps ({ // 这里输入你要修改的组件style
41+ height: 48 ,
42+ backgroundColor: ' red'
43+ });
44+ },
45+ render () {
46+ return (
47+ < View ref= {component => this ._root = component} {... this .props } style= {styles .button }>
48+ < Text > {this .props .label }< / Text >
49+ < / View>
50+ )
51+ },
52+ });
53+ ```
54+
55+ ### 避免和` render ` 方法的冲突
56+
57+ 如果要更新一个由` render ` 方法来维护的属性,则可能会碰到一些出人意料的bug。因为每一次组件重新渲染都可能引起属性变化,这样一来,之前通过` setNativeProps ` 所设定的值就被完全忽略和覆盖掉了。
58+
59+
2560D4: React Native 函数的绑定 (2016-8-23)
2661----
2762在ES6的class中函数不再被自动绑定,你需要手动去绑定它们。
You can’t perform that action at this time.
0 commit comments