Javascript 在react native中提交值后如何关闭弹出窗口?
我正在使用Javascript 在react native中提交值后如何关闭弹出窗口?,javascript,reactjs,react-native,popup,jsx,Javascript,Reactjs,React Native,Popup,Jsx,我正在使用反应本机弹出对话框。弹出窗口中只有一个按钮(yes)。我想在向服务器提交值的同时关闭按钮。现在,单击yes按钮后,获取提交到服务器的值。如何以相同的onPress方法编写close函数?下面是我的代码 onPressYes = (workType) => { AsyncStorage.getItem('userid').then((usid) =>{ this.setState({ 'userid': us
反应本机弹出对话框
。弹出窗口中只有一个按钮(yes
)。我想在向服务器提交值的同时关闭按钮。现在,单击yes
按钮后,获取提交到服务器的值。如何以相同的onPress方法编写close函数?下面是我的代码
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>
);
onPressYes=(工作类型)=>{
AsyncStorage.getItem('userid')。然后((usid)=>{
这是我的国家({
“用户ID”:usid
});
console.log(usid);
获取(全局用户请求){
方法:'POST',
标题:{
“接受”:“应用程序/json”,
“内容类型”:“应用程序/json”,
},
正文:JSON.stringify({
工作类型,
usid
})
})
.then(response=>response.json())
.然后((响应数据)=>{
这是我的国家({
数据来源:responseData
});
});
})
}
popUpDialog=(id,工作类型)=>{
这是我的国家({
工作类型:工作类型
});
这个.popupDialog.show();
}
render(){
返回(
{
this.popupDialog=popupDialog;
}}
dialogStyle={{{backgroundColor:#FFFFFF],高度:180,宽度:300,边框宽度:1,填充:10}}
OverlyBackgroundColor=“#fff”onDismissed={()=>{
}}>
您确定要提交吗?
this.onPressYes(工作类型)}/>
);
使用可见的道具控制它
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>
import Dialog,{DialogContent}来自“react native popup Dialog”;
从“react native”导入{Button}
{
this.setState({visible:true});//此处
}}
/>
{
this.setState({visible:false});
}}
>
{...}
Ref:根据您的代码,您可以使用this.popupDialog.dislose()
实例方法隐藏对话框:
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
});
});
})
}