React Native:如何在单击按钮时获取文本值? [英] React native:how to get Text value on a button click?

查看:45
本文介绍了React Native:如何在单击按钮时获取文本值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Text,我想在点击时获取文本值.

I have a Text, and I want to get text value on click.

例如

click()
{
    // how to get text value here 
}

<Text style={{color: 'red,textAlign:'center'}} onPress={this.click.bind(this)}>
    Name
</Text> 

推荐答案

您可以使用 ref 属性来访问 Text 的值.

You may use ref attribute to access Text's value.

<Text ref='myText'>This is my text</Text>
<Button onPress={()=>alert(this.refs.myText.props.children)} title='Press Me'/>

这篇关于React Native:如何在单击按钮时获取文本值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆