setNativeProps文本组件的更改值React Native Direct Manipulation

Shi*_*nha 7 react-native

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

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中更新的原因?

Shi*_*nha 9

而不是试图改变<Text>组件的内容.我只是替换<TextInput editable={false} defaultValue={this.state.initValue} />并保持其余代码相同.如果有人知道如何改变<Text>使用setNativePropsOR的其他方法直接操作的价值.发布答案,恶意审查并接受.

  • 你不能直接操作`<Text>`因为它使用`NSTextStorage`而不是`NSString`属性.看看这个:https://github.com/facebook/react-native/blob/master/Libraries/Text/RCTText.h#L15如果有办法将字符串值转换为`NSTextStorage`值,那么我们也可以操纵文本字段,但据我所知,我们不能. (3认同)