1

I wanted to clear Text Component using ref on timeout but can't find.

<Text style={{ color: "#fff" }} ref={text => this._text = text}>
              {isKeyValid === undefined
                ? null
                : isKeyValid
                ? "Key is been verified."
                : "Your key is invalid or expired."}
</Text>

clearErrorMessages = () => {
   setTimeout(() => {
   //something like that here which i have no idea  
   this._text.clear() 
   }, 2000);
}

Any idea about this guys thanks...

1
  • 1
    I don't think text component has clear() method, it's actually for TextInput. You can easily tackle this issue by using state. Commented Apr 4, 2019 at 10:49

2 Answers 2

2

There is no method to clear text in a <Text> component. You can do this: take whatever value you want to display in state variable and set it to empty string when required.

in constructor:

this.state={
  value:"myvalue",
}

in render

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

then on any action/function you want to clear the text do this:

this.setState({value:""});
Sign up to request clarification or add additional context in comments.

Comments

1

You can approach, different method then refs,

take one state and make it false by default in a constructor, and set it to true after time out. Also, use it in your a condition see this,

<Text style={{ color: "#fff" }} ref={text => this._text = text}>
              {isKeyValid === undefined && this.state.isCleared
                ? null
                : isKeyValid
                ? "Key is been verified."
                : "Your key is invalid or expired."}
</Text>

clearErrorMessages = () => {
   setTimeout(() => {
   //something like that here which i have no idea  
   this.setState({isCleared:true});
   }, 2000);
}

Don't forget to initialize it in constructor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.