我有一个模态组件,应该在 setState 更改时调用它,但由于某种原因它没有更新。在第一个文件中,我在渲染中设置了以下内容。
<ModalParcel showModal={this.state.showModal} parcelToConfirm={this.state.dataForParcelToConfirm} />
在模态组件构造函数()中,我设置了以下内容。
constructor(props) {
super(props);
console.log("Constructor for modal componenet called.")
//this.renderRowForMain = this.renderRowForMain.bind(this);
this.state = {
show: this.props.showModal
};
}
render() {
if(this.state.show == true){
var content =
<View style={styles.modal}>
<View style={styles.modalContent}>
<TouchableHighlight onPress={() => this.closeModal()}>
<Text>Close</Text>
</TouchableHighlight>
</View>
</View>;
}else {
var content = null;
}
return ( content );
}
问题是构造函数() 只在初始创建时被调用一次。我的印象是,当我更新状态以显示模态时,构造函数将再次被调用,但这并没有发生。
我基本上想更改状态以显示模态,然后重新运行 render()。