React Native TextInput value is not working

Viewed 5631

I have something like this:

<TextInput
  style={{height: 40, borderColor: 'gray', borderWidth: 1}}
  value={0}
  onChangeText={(input) => this.setState({ value: input })}
/>

However, the input box is always empty after load, any ideas?

5 Answers

TextInput component only accept strings. Looks like you have a integer there. Try changing that to a string. Heres a link to the doc.

Its caused by an integer value. Do JSON.stringify(value)

just change the value = {this.state.value}

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

use toString()

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

Try this it works with me!

    const [_employee, setEmployee]=useState(
      {
        name: "name"
        salary: 0
      })

. . .

    <Input
      onChangeText={(salary) =>
        (salary = parseInt(salary)) & setEmployee({ ..._employee, salary })
      }
      value={JSON.stringify(_employee.salary)}
    />
Related