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

时间:2018-11-07 06:08:32

标签: javascript reactjs react-native popup jsx

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

2 个答案:

答案 0 :(得分: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
    });
    });
      })
}

答案 1 :(得分:0)

使用pre_delete道具来控制它。

from threading import Thread
import pyttsx3
engine = pyttsx3.init()
from guizero import App, TextBox, PushButton, Text, 
alerts,Slider
from tkinter.font import Fon

def voice_speed(slider_value):
     r=slider_value
     print(r)

def voice_volume(slider_value):
     v=slider_value
     print(v)


def go():
    while True:
         #print(slider_r.value)
         print(slider_v.value)
         engine.setProperty('rate',slider_r.value)
         engine.setProperty('volume',slider_v.value)
         engine.say(textbox.value)
         engine.runAndWait()




app = App()
slider_r = Slider(app, command=voice_speed)
slider_v = Slider(app, command=voice_volume)

text = Text(app, text="Enter your name")
textbox = TextBox(app)
button = PushButton(app, text="Start", command=go)
app.display()

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