Search code examples
react-nativetextinput

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.


Solution

  • 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: ""
     }
    }