Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native setNativeProps更改文本组件React本机直接操作的值_React Native - Fatal编程技术网

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

React native setNativeProps更改文本组件React本机直接操作的值,react-native,React Native,由于性能原因,我希望直接更新组件的值 render(){ <View> <Text style={styles.welcome} ref={component => this._text = component}> Some Text </Text> <TouchableHighlight underlayColor='#88D4F5' style={styles.button}> &

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

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>
}
从本质上讲,尝试遵循本示例中的相同方法:

编辑: 当我尝试显式分配更新的值时:

this._text.props.children = "updated"; 
(我知道这是在RN做事的正确方式)。我收到错误“无法分配给对象“#”的只读属性“children”


因此,可能这就是为什么它由于某种原因无法在RN中更新?

而不是尝试更改
组件的内容。我只是用
替换,并保持代码的其余部分不变。如果有人知道如何使用
setNativeProps
或其他直接操作方法更改
的值。发布答案,我会审核并接受。

文本标签没有
text
prop,所以

this._text.setNativeProps({ text: 'XXXX' })
this._text.setNativeProps({ style: { color: 'red' } })  
不起作用

但是文本标记有一个
样式
属性,所以

this._text.setNativeProps({ text: 'XXXX' })
this._text.setNativeProps({ style: { color: 'red' } })  

有效。

由于setNativeProps无法解决更改
内容的目的,我使用了以下方法,效果良好。创建简单的反应组件,如下所示

var Txt = React.createClass({
getInitialState:function(){
    return {text:this.props.children};
},setText:function(txt){
    this.setState({text:txt});
}
,
render:function(){
    return <Text {...this.props}>{this.state.text}</Text>
}
});
var Txt=React.createClass({
getInitialState:函数(){
返回{text:this.props.children};
},setText:function(txt){
this.setState({text:txt});
}
,
render:function(){
返回{this.state.text}
}
});

我们不能在
文本
组件上使用
setNativeProps
,相反,我们可以解决方法并通过使用
TextInput
代替
文本
来实现相同的结果

通过将
pointerEvent='none'
放在封闭的
视图上,我们禁用了单击,因此我们无法编辑
TextInput
(您也可以在
TextInput
中设置
editable={false}
,以取消编辑)

演示-计时器(每1秒后计数变化)

import React,{Component}来自'React';
从“react native”导入{TextInput,样式表,视图};
类Demo扩展了组件{
componentDidMount(){
让计数=0;
设置间隔(()=>{
计数++;
如果(本参考文件){
this.ref.setNativeProps({text:count.toString()});
}
}, 1000);
}
render(){
返回(
(this.ref=ref)}
defaultValue={0'}
//可编辑={false}
style={style.textInput}
/>
);
}
}
const styles=StyleSheet.create({
容器:{
弹性系数:0.7,
为内容辩护:“中心”,
对齐项目:“居中”,
},
文本输入:{
尺寸:60,
宽度:“50%”,
边框颜色:“灰色”,
边框宽度:1,
方面:1,
边界半径:8,
填充:5,
textAlign:'中心',
},
});
导出默认演示;

不能直接操作
,因为它使用
NSTextStorage
而不是
NSString
属性。检查一下:如果有一种方法可以将字符串值转换为
NSTextStorage
值,那么我们也可以操作文本字段,但据我所知,我们不能。你的意思是
var Text
还是
return我想这正是问题中试图避免的。使用
TextInput
,并用
this设置值。_Text.setNativeProps({Text:'Hi There!')