[英]Get text from TextView and copy it to clipboard in react native
我想通过单击图标来复制 textview 中的值。 这是我到目前为止所做的:
render() {
return (
<View style={{marginTop: 50, marginLeft: 50}}>
<View>
<Text>Логин:</Text>
<Text ref='myText'>45645546654</Text>
</View>
<TouchableOpacity onPress={() => Clipboard.setString(this.refs.myText.props.children)}>
<View>
<MaterialIcons
name='content-copy'
size={21}
/>
</View>
</TouchableOpacity>
</View>
);
}
工作区: https://snack.expo.io/@jasurkurbanov/updated2
它有点在小吃上工作。 但是当我在手机上运行它时,我得到了错误。
未定义不是 object('评估'_this.refs.myText.props.children')
找到答案。 问题出在构造函数上。 我没有创建它,也没有定义 state。 请访问https://snack.expo.io/@jasurkurbanov/updated2
您应该在constructor()
上定义您的 refs 变量。
例如:
constructor() {
...
this.textViewRefs = React.createRef();
...
}
render() {
return (
...
<Text ref={this.textViewRefs}>45645546654</Text>
...
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.