由于性能原因,我想直接更新组件的值.
render(){
<View>
<Text style={styles.welcome} ref={component => this._text = component}>
Some Text
</Text>
<TouchableHighlight underlayColor='#88D4F5'
style={styles.button}>
<View>
<Text style={styles.buttonText}
onPress={this.useNativePropsToUpdate.bind(this)}>
Iam the Child
</Text>
</View>
</TouchableHighlight>
</View>
}
Run Code Online (Sandbox Code Playgroud)
这是我用来更新文本组件的方法.我不知道我是否正在设置正确的属性/如何确定要设置的属性:
useNativePropsToUpdate(){
this._text.setNativeProps({text: 'Updated using native props'});
}
Run Code Online (Sandbox Code Playgroud)
本质上尝试遵循此示例中的相同方法:https: //rnplay.org/plays/pOI9bA
编辑:当我尝试显式分配更新的值时:
this._text.props.children = "updated";
Run Code Online (Sandbox Code Playgroud)
(我知道这是在RN中做事的正确方法).我收到错误"无法分配给对象'#'的只读属性'子''
所以也许这就是为什么它因某些原因无法在RN中更新的原因?
而不是试图改变<Text>
组件的内容.我只是替换<TextInput editable={false} defaultValue={this.state.initValue} />
并保持其余代码相同.如果有人知道如何改变<Text>
使用setNativeProps
OR的其他方法直接操作的价值.发布答案,恶意审查并接受.
归档时间: |
|
查看次数: |
11500 次 |
最近记录: |