如何在 React Native 中提交值后关闭弹出窗口?

How to close popup after submitting values in react native?

我正在使用 react-native-popup-dialog。弹出窗口中只有一个按钮 (yes)。我想同时关闭按钮 我想在单击 yes 按钮后将值提交给 server.Now 将值提交到服务器。如何在同一个 onPress 方法中编写关闭函数?以下是我的代码

onPressYes = (workType) => {
            AsyncStorage.getItem('userid').then((usid) =>{
          this.setState({
            'userid': usid
          });
          console.log(usid);
         fetch(GLOBAL.USER_REQUEST,{
           method:'POST',
           headers:{
             'Accept': 'application/json',
             'Content-Type': 'application/json',

           },
           body:  JSON.stringify({
            workType,
            usid
             })
         })
         .then(response => response.json())
         .then((responseData) => {
           this.setState({
           data:responseData
         });
         });
            })
     }

popUpDialog = (id, workType) => {
           this.setState ({
            workType: workType
         });
         this.popupDialog.show();

       }
render(){
  return(
      <PopupDialog ref={popupDialog => {
                           this.popupDialog = popupDialog;
                         }}
                        dialogStyle={{ backgroundColor: "#FFFFFF", height: 180, width:300, borderWidth:1,padding:10}}
                        overlayBackgroundColor="#fff"  onDismissed={() => {
  }}>
                          <View style={styles.dialogContentView}>
                            <Text style={{fontSize:18, margingTop:10,color:"#000000"}}>Are you sure you want to submit?</Text>
                            <View style={{alignSelf:'center'}}>

                              <View style={styles.button_1}>
                                <Button title="Yes" color="#8470ff" onPress={() => this.onPressYes(workType)}/>
                              </View>
);

使用visible道具控制

import Dialog, { DialogContent } from 'react-native-popup-dialog';
import { Button } from 'react-native'

<View style={styles.container}>
  <Button
    title="Show Dialog"
    onPress={() => {
      this.setState({ visible: true }); // here
    }}
  />
  <Dialog
    visible={this.state.visible} // here
    onTouchOutside={() => {
      this.setState({ visible: false });
    }}
  >
    <DialogContent>
      {...}
    </DialogContent>
  </Dialog>
</View>

参考:https://github.com/jacklam718/react-native-popup-dialog

根据您的代码,您可以使用this.popupDialog.dismiss()实例方法来隐藏对话框:

onPressYes = (workType) => {
    this.popupDialog.dismiss(); // action to close a dialog

    AsyncStorage.getItem('userid').then((usid) =>{
    this.setState({
      'userid': usid
    });
    console.log(usid);
    fetch(GLOBAL.USER_REQUEST,{
      method:'POST',
      headers:{
        'Accept': 'application/json',
        'Content-Type': 'application/json',

      },
      body:  JSON.stringify({
      workType,
      usid
        })
    })
    .then(response => response.json())
    .then((responseData) => {
      this.setState({
      data:responseData
    });
    });
      })
}