简体   繁体   English

从 TextView 获取文本并将其复制到剪贴板以响应本机

[英]Get text from TextView and copy it to clipboard in react native

I want to copy value inside textview by clicking to icon.我想通过单击图标来复制 textview 中的值。 This is what I've done so far:这是我到目前为止所做的:

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

Workspace: https://snack.expo.io/@jasurkurbanov/updated2工作区: https://snack.expo.io/@jasurkurbanov/updated2

It is somewhat working on snack.它有点在小吃上工作。 But what when I am running it on my phone I am getting error.但是当我在手机上运行它时,我得到了错误。

Undefinied is not an object ('evaluating '_this.refs.myText.props.children')未定义不是 object('评估'_this.refs.myText.props.children')

Found answer.找到答案。 The problem was with the constructor.问题出在构造函数上。 I didn't create it and not defined the state.我没有创建它,也没有定义 state。 Please visit https://snack.expo.io/@jasurkurbanov/updated2请访问https://snack.expo.io/@jasurkurbanov/updated2

You should defined your refs variable on constructor() .您应该在constructor()上定义您的 refs 变量。

for example:例如:

constructor() {
...
    this.textViewRefs = React.createRef();
...
}

render() {
    return (
...
        <Text ref={this.textViewRefs}>45645546654</Text>
...
    )
}

look this https://reactjs.org/docs/forwarding-refs.html for further information查看此https://reactjs.org/docs/forwarding-refs.html了解更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM