如何清除TextInput"占位符"当用户点击输入?

时间:2017-09-16 12:19:03

标签: react-native textinput

我想知道如何清除"占位符"当用户单击该字段以填充它时,在TextInput中。

这是我的TextInput:

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

我的构造函数:

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

提前谢谢。

2 个答案:

答案 0 :(得分:2)

使用值TextInput placeholder

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

并在构造函数

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

答案 1 :(得分:2)

你应该像这样使用TextInput

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: '')}
    />

当然,最好在类的主体中定义render函数之外的函数,并在JSX中使用它们。