David Massey
David Massey

Reputation: 319

How can I access the text inside of a Text component?

Given something like this:

<Text>Hello</Text>

Is there any way to access the text inside this Component?

Here is a specific example where I would like to connect the text inside a Text component to the press of a TouchableOpacity. The deletekey function in this example needs to get "Hello" as a parameter.

                <View>
                    <TouchableOpacity onPress={() => this.deleteKey()}>
                        <EvilIcon
                            name='close'
                            type='evilicon'
                            color='#517fa4'
                            size={15}
                        />
                    </TouchableOpacity>
                    <Text>Hello</Text>
                </View>

Upvotes: 2

Views: 96

Answers (1)

Ravi
Ravi

Reputation: 35589

You can use ref of Text

<Text ref='helloText'>Hello</Text>

and onPress of TouchableOpacity

this.refs.helloText.props.children

If you have dynamic text inside <Text> you should use state

<Text>{this.state.textValue}</Text>

and to set value in it, you need to use this.setState({textValue:'Hello'})

Upvotes: 2

Related Questions