CoderLim
CoderLim

Reputation: 1232

How can I get value of Text Component?

<Text>aircraft</Text>

I need to get aircraft in Text, and change the value of Text dynamically. How could I do?

Upvotes: 0

Views: 1021

Answers (2)

Samuli Hakoniemi
Samuli Hakoniemi

Reputation: 19049

You can access it like this (example: https://rnplay.org/apps/ACHJEQ)

<Text ref={(elem) => this.textElem = elem}>Hello world!</Text>

and then: console.log('textElem content', this.textElem.props.children);

But you can't set it since it's a (read-only) prop.

Upvotes: 3

dv3
dv3

Reputation: 4579

Well... various ways to do this.

For example:

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

and then just change the state variable. You could also implement it sth like:

<Text>{ (this.state.checkIfTrue) ? 'Boeing787' : 'Airbus 320' } </Text>

this checks if this.state.checkIfTrue results to true and displays either 'Boeing787' or 'Airbus 320'.

This should give you a first idea.

Upvotes: 0

Related Questions