【问题标题】:How to close popup after submitting values in react native?react native提交值后如何关闭弹出窗口?
【发布时间】:2019-04-10 13:56:18
【问题描述】:

我正在使用react-native-popup-dialog。弹出窗口中有一个按钮 (yes)。我想关闭按钮同时我想向服务器提交值。现在点击yes 按钮值后提交到服务器。如何在同一个 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>
);

【问题讨论】:

    标签: javascript reactjs react-native popup jsx


    【解决方案1】:

    使用visible props 来控制它。

    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

    【讨论】:

      【解决方案2】:

      根据你的代码,你可以使用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
          });
          });
            })
      }
      

      【讨论】:

        猜你喜欢
        • 2012-01-26
        • 2015-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多