Amaury Leproux
Amaury Leproux

Reputation: 229

How to clear TextInput "placeholder" when user click on the input?

I would like to know how I can clear the "placeholder" in a TextInput when a user click on the field to fill it.

This is my TextInput:

 <TextInput 
   style={{height:40, borderColor: 'gray', borderWidth:1}}
   onChangeText={(text) => this.setStoreName(text)}
   value={this.state.storeName}
 />

And my constructor:

 constructor(props) {
    super(props)
    this.state = { 
        name: "Votre nom", 
        storeName: "Le nom de votre commerce", 
        email: "Votre email", 
        address: "L'adresse de votre commerce", 
        city: "Votre ville", 
        category: "Categorie", 
        password: "Votre mot de passe"
    }
}

Thank you in advance.

Upvotes: 7

Views: 8766

Answers (2)

Vahid Boreiri
Vahid Boreiri

Reputation: 3438

You should use the TextInput like this:

constructor(props) {
 super(props)
 this.state = { 
    name: "", 
    storeName: "", 
    email: "", 
    address: "", 
    city: "", 
    category: "", 
    password: "",
    placeholder: "initial value"
 }
}    

   <TextInput 
     style={{height:40, borderColor: 'gray', borderWidth:1}}
     onChangeText={(text) => this.setStoreName(text)}
     value={this.state.storeName}
     placeholder={this.state.placeholder}
     onFocus={() => this.setState(placeholder: '')}
    />

Of course, it's better to define your functions outside of render function in the body of class and use them in the JSX.

Upvotes: 1

Mohamed Khalil
Mohamed Khalil

Reputation: 3126

use placeholder attribute with value TextInput placeholder

<TextInput 
 style={{height:40, borderColor: 'gray', borderWidth:1}}
 onChangeText={(text) => this.setStoreName(text)}
 value={this.state.storeName}
 placeholder="Le nom de votre commerce"
/>

and in constructor

constructor(props) {
 super(props)
 this.state = { 
    name: "", 
    storeName: "", 
    email: "", 
    address: "", 
    city: "", 
    category: "", 
    password: ""
 }
}

Upvotes: 3

Related Questions