Ramiz Ansari
Ramiz Ansari

Reputation: 524

React native TextInput not clearing default value when user start Typing

According to docs on React-Native's website:

defaultValue: Provides an initial value that will change when the user starts typing. Useful for simple use-cases where you do not want to deal with listening to events and updating the value prop to keep the controlled state in sync.

but this is not the case happening in Android API 16. When I tap the textinput component in my app, the default value remains there and is not cleared. how to fix this issue? am i doing something wrong? my code:

<TextInput style={styles.input} defaultValue='Enter Password'/>

how to clear the default value when user starts typing?

Upvotes: 0

Views: 2760

Answers (3)

Bethel Eyo
Bethel Eyo

Reputation: 21

if you have an initial value that you want to display once the screen loads and you want to be able to edit this initial value and update it with a new value.

First, you have to set the initial value with the value property and then use onChangeText to update the initial value to a new value

<InputField
          onChangeText={text => this.setState({value: text})}
          value={this.state.value}
        />

in your case InputField will be TextInput

Upvotes: 1

Wanda Ichsanul Isra
Wanda Ichsanul Isra

Reputation: 2292

In my understanding, what you're trying to do is just set the placeholder, not the defaultValue. You can do it this way.

<TextInput style={styles.input} placeholder='Enter Password' onChange={text => this.setState({input: text})} />

Upvotes: 3

digit
digit

Reputation: 4565

You need to set new value using onChangeText and value prop

Here is a simple eg:

<TextInput style={styles.input} defaultValue='Enter Password' 
onChangeText={text => this.setState({input: text})} value={this.state.input}/>

Upvotes: 3

Related Questions