[英]Dialog not closing react native
I am using the React native popup dialog package. 我正在使用React本机弹出对话框包。 I'm trying to close the dialog onPress
and then navigate away from the screen. 我正在尝试关闭onPress
对话框,然后离开屏幕。 The navigation is happening, but the dialog remains open. 正在进行导航,但是对话框保持打开状态。 And a warning Can't perform a react state update on an unmounted component
is shown. 并显示警告“ 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>
The navigate()
function navigate()
函数
navigate = () => {
const { navigation } = this.props;
this.setState({dialogVisible:false})
navigation.navigate('Home');
}
setState
is async
, which does not block the execution. setState
是async
,不会阻止执行。 When setState
gets called next line executes ( setState
is yet to complete) and you got navigated to Home
. 调用setState
将执行下一行( setState
尚未完成),并且您已导航到Home
。
You can you callback in setState
for navigation. 您可以在setState
回调以进行导航。
navigate = () => {
const { navigation } = this.props;
this.setState({dialogVisible:false}, () => navigation.navigate('Home'))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.