我正在使用 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>
);
最佳答案
根据你的代码,你可以使用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
});
});
})
}
关于javascript - 如何在 React Native 中提交值后关闭弹出窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53184347/