1

Which lifecycle method I should use to set the existing value in a form? I can't do <input value={this.props.title} /> because I want to make input as a controlled component.

I can do a promise then after the async method but will this be anti pattern?

componentDidMount() {
  this.props.fetchItem(this.itemId)
  .then(v=>{this.setState({title: v.title})})
}
Jenny Mok
  • 2,744
  • 9
  • 27
  • 58

1 Answers1

1

You can use componentWillReceiveProps() method to set your props value to state.

Something like this.

componentWillReceiveProps(nextProps){
 // store your props value to state here
if(nextProps.formData !== undefined){
  this.setState({ formData: nextProps.formData });
  }

}

To update that state value on input change method

handleChange = (name, value) => {
  // update state here
  this.setState({ [name]: value });
  }
Vikram Thakur
  • 2,329
  • 1
  • 11
  • 16