score:2

Accepted answer

state initialization is run only once when component is mounted and the props.contact is undefined yet. that is why you got undefined in your render.

you need to add componentdidupdate lifecycle method if you want to update your child component state based on props

  componentdidupdate(prevprop) {
    if (this.props.contact !== prevprops.contact) {
      this.setstate({ data: this.props.contact });
    }
  }

the other thing is that if you don't need to change this.props.contact in your child component, it's way better to use the prop directly instead of setting state which is not gonna change internally.


Related Query

More Query from same tag