gpt4 book ai didi

react-native - setNativeProps更改文本组件的值React native 直接操作

转载 作者:行者123 更新时间:2023-12-03 14:10:33 31 4
gpt4 key购买 nike

由于性能原因,我想直接更新组件的值。

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>
}

这是我用来更新文本组件的方法。我不知道是否要设置正确的属性/如何确定要设置的属性:
  useNativePropsToUpdate(){
this._text.setNativeProps({text: 'Updated using native props'});
}

本质上,该示例尝试遵循相同的方法:
https://rnplay.org/plays/pOI9bA

编辑:
当我尝试显式分配更新后的值时:
this._text.props.children = "updated"; 

(我知道这是在RN中做事的正确方法)。我收到错误消息“无法分配为只读对象'#'的属性'子代'”

因此,也许这就是为什么出于某种原因而无法在RN中对其进行更新吗?

最佳答案

而不是尝试更改<Text>组件的内容。我只是替换为<TextInput editable={false} defaultValue={this.state.initValue} />,其余代码保持不变。如果有人知道如何使用<Text>或其他直接操作方法来更改setNativeProps的值。发布答案,并进行不良审查并接受。

关于react-native - setNativeProps更改文本组件的值React native 直接操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37016664/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com