答案 0 :(得分:2)
当你浏览文档时,它会说明setting props to the initial state is an anti pattern
。因此,您可以在componentDidMount()
生命周期函数
constructor(props) {
super(props);
this.state = {
birthdate: null,
name: null,
contact: null,
gender: null,
photoUrl: 'https://facebook.github.io/react/img/logo_og.png'
}
}
componentDidMount() {
const {birthdate, name, contact, gender, photoUrl} = this.props;
this.setState({
birthdate: birthdate,
name: name,
gender: gender,
contact: contact,
photoUrl: photoUrl
})
}