我正在使用React native popup dialog软件包。我正在尝试关闭对话框onPress
,然后离开屏幕。正在进行导航,但是对话框保持打开状态。并显示警告Can't perform a react state update on an unmounted component
。
<Dialog
dialogStyle = {{textAlign:'center'}}
visible={this.state.dialogVisible}
height = {150}
dialogAnimation={new SlideAnimation({
slideFrom: 'bottom',
})}
>
<DialogTitle
title = {"Information"}
textStyle = {{textAlign:'center',fontFamily:'raleway-regular',color:'#4abce3'}}
/>
<DialogContent style = {{justifyContent:'center',alignContent:'center'}}>
<Text>{this.state.requestResult}</Text>
<Button
title = "Okay!"
onPress = {()=>{ this.navigate() }}
type = "outline"
buttonStyle = {styles.dialogButton}
titleStyle = {styles.choiceButtonTitle}
/>
</DialogContent>
</Dialog>
navigate()
功能
navigate = () => {
const { navigation } = this.props;
this.setState({dialogVisible:false})
navigation.navigate('Home');
}
答案 0 :(得分:1)
setState
是async
,这不会阻止执行。当setState
被调用时,将执行下一行(setState
尚未完成),并且您已导航到Home
。
您可以在setState
中回调以进行导航。
navigate = () => {
const { navigation } = this.props;
this.setState({dialogVisible:false}, () => navigation.navigate('Home'))
}