对话框未关闭本机反应

时间:2019-08-28 11:56:32

标签: javascript react-native

我正在使用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'); 
}

1 个答案:

答案 0 :(得分:1)

setStateasync,这不会阻止执行。当setState被调用时,将执行下一行(setState尚未完成),并且您已导航到Home

您可以在setState中回调以进行导航。

navigate = () => {
      const { navigation } = this.props;
      this.setState({dialogVisible:false}, () => navigation.navigate('Home')) 
}