6

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?

Brian Law
  • 460
  • 1
  • 4
  • 18

5 Answers5

24

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

Harshana
  • 533
  • 4
  • 9
1

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

Yinka
  • 1,741
  • 4
  • 15
  • 23
0

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 })}
/>
0

use toString()

<TextInput
  style={{height: 40, borderColor: 'gray', borderWidth: 1}}
  value={this.state.value.toString()}
  onChangeText={(input) => this.setState({ value: input })}
/>
Mitesh K
  • 694
  • 1
  • 11
  • 24
-1

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)}
    />
  • Welcome to stackoverflow and thank you for your effort. Unfortunately, your answer is not very helpful. Please explain your answer and avoid code-only answers. To learn more, see our tips on [writing great answers](https://stackoverflow.com/help/how-to-answer) – Luke_ Feb 19 '21 at 12:43