1

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} />
3
  • spentHours should be a key. when you set the state, you should set it as this.setState({spentHours: spentHours}) Commented Sep 29, 2016 at 7:37
  • @JanFranzPalngipang thats just shorthand in es6 - take a look here: es6-features.org/#PropertyShorthand. If the var name is the same as the key it can be used like this :) Commented Sep 29, 2016 at 8:29
  • I got the the answer. I have to use string for displaying the value in TextInput Commented Sep 29, 2016 at 10:20

1 Answer 1

1

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()}
/>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.