【问题标题】:React-native-popup-dialog is not closing after page changes页面更改后 React-native-popup-dialog 未关闭
【发布时间】:2021-11-06 18:11:02
【问题描述】:

我将这个包用于对话框。当用户进入时,我得到的一些页面会自动打开这个对话框。问题是当用户点击另一个屏幕按钮时(对话框正在打开)。应用导航到另一个屏幕,对话框仍然打开。

import Dialog, {
  DialogFooter,
  DialogButton,
  DialogContent,
  DialogTitle,
} from 'react-native-popup-dialog';

constructor(props) {
    super(props);
    this.state = {
    
      controlDialog: false, 
    };
  }

在模糊中我关闭它但没有任何反应

  componentDidMount() {
    const t = this;
    this._unsubscribe = this.props.navigation.addListener('blur', () => {
      this.setState({controlDialog:false})

    });

在渲染对话框中

<Dialog
  visible={this.state.controlDialog}
  width={0.8}
  footer={
    <DialogFooter style={{ paddingVertical: 10 }}>
      <DialogButton
        textStyle={{
          fontSize: 16,
          fontWeight: "normal",
          color: "white",
        }}  
        text="Later"
        onPress={() => {
          this.setState({ controlDialog: false }, () => {});
        }}
      />
      <DialogButton
        textStyle={{
          fontSize: 16,
          fontWeight: "normal",
          color: "white",
        }}  
        text="OK"
        onPress={() => {}}
      />
    </DialogFooter>
  }
>
  <DialogContent></DialogContent>
</Dialog>;

什么尝试

componentDidUnmount() {
    this.setState({controlDialog: false});
    alert('1');
  }
  componentWillUnmount() {
    this.setState({controlDialog: false});

    alert('2');
  }

标签栏

function TabBar({state, descriptors, navigation}) {
  return (
    <Box
      pb={Platform.OS=='android'?10:25}
      bg="white"
      flexDirection="row"
      style={{
        shadowColor: '#000',
        shadowOpacity: 0.1,
        shadowRadius: 20,
      }}>
      {state.routes.map((route, index) => {
        const {options} = descriptors[route.key];

        const label =
          options.tabBarLabel !== undefined
            ? options.tabBarLabel
            : options.title !== undefined
            ? options.title
            : route.name;

        const isFocused = state.index === index;

        const onPress = () => {
          const event = navigation.emit({
            type: 'tabPress',
            target: route.key,
          });

          if (!isFocused && !event.defaultPrevented) {
            NavigationService.reset(route.name);
          }
        };

....

【问题讨论】:

    标签: react-native dialog modal-dialog


    【解决方案1】:

    更改类生命周期的 Dialog componentDidUnmount() 方法。当您离开屏幕时会调用此方法。

      componentDidUnmount() {
      
         this.setState({controlDialog:false})    
    
       }
    

    【讨论】:

    • 我试过一次componentWillUnmount,但不值得
    • 您是否使用底部标签栏导航?
    • 不是默认标签栏。我得到了名为 Tabbar 的 js 文件。我添加到问题中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多