Vikiesakki
Vikiesakki

Reputation: 31

TextInput is displaying the value react native

Hi in my componentWillMount I set my states like this

componentWillMount(){
    this.timeSheetData().then((timeSheetResponse)=>{
        this.setState({comments:timeSheetResponse.comments});
        this.setState({spentHours:parseInt(timeSheetResponse.hours)});
        alert(this.state.spentHours);

    });
});

In my view

I have the TextInput like this. I can't display the value on the TextInput but I can display the value on Text I don't know why it's happening

<TextInput keyboardType='numeric' onChangeText={(spentHours) =>this.setState({spentHours})} 
                   value={this.state.spentHours} />

Upvotes: 1

Views: 1693

Answers (1)

Tyler
Tyler

Reputation: 1662

Input values need to be strings so you either need to remove parseInt in your componentWillMount method:

this.setState({ spentHours: timeSheetResponse.hours });

or in your template you need to convert the input value to a string with toString():

<TextInput
  keyboardType='numeric'
  onChangeText={spentHours => this.setState({ spentHours })} 
  value={this.state.spentHours.toString()}
/>

Upvotes: 1

Related Questions