Daan Luttik
Daan Luttik

Reputation: 2855

Change content of Text element React

This seems so basic but I can't figure out to do it, and it doesn't seem to be documented anywhere. I've got a text element in my react page

<Text ref="WarningText"
                  style={loginStyle.warningLabel}>{this.warningText}</Text>

And I want to change the content of this Text element. Changing this.warningText doesn't help nor does calling this.refs.WarningText.setNativeProps({text: text}); or changing any other propname that I could come up with.

Any help would be great, thanks.

Upvotes: 1

Views: 5137

Answers (1)

abeikverdi
abeikverdi

Reputation: 1226

Better to use state instead of refs or anything else. That's Reacts design pattern.

<Text style={loginStyle.warningLabel}>{this.state.warningText}</Text>

and in your event you can change the text by chnaging the state and react automatically updates your text.

this.setState({warningText: "some text"})

Here's some more documentation on setState. It gives most of the ins and outs of how to use and not use state in your components.

Upvotes: 3

Related Questions