【发布时间】:2018-06-22 03:44:36
【问题描述】:
我有一个二维码扫描器,根据服务器的响应,我想显示一个具有不同背景颜色的模式!
我使用了react-native-qrcode-scanner 和react-native-modalbox。
App.js:
render() {
return (
<View style={styles.container}>
<Header headerText="Scan ????" />
<View style={styles.cameraContainer}>
<QRCodeScanner
ref={node => {
this.scanner = node;
}}
onRead={this.onRead.bind(this)}
cameraStyle={{ height: "100%" }}
/>
</View>
{this.renderModal()}
</View>
);
}
然后我们有 onRead() 函数,即:
onRead(e) {
// send request to server and receive the response!
switch (msg from server){
case "IN": {
userStatus = "1";
}
case "OUT": {
userStatus = "0";
}
case "Error": {
userStatus = "-1";
}
this.refs.myModal.open();
}
最后是 renderModal():
renderModal() {
switch (userStatus) {
case "1": {
(modalBackgroundColor = "#90EE90"), (modalText = "In");
}
case "0": {
(modalBackgroundColor = "#87CEFA"), (modalText = "Out");
}
case "-1": {
(modalBackgroundColor = "#FF0000"), (modalText = "Error");
}
}
return (
<Modal
style={styles.modalStyle}
position={"center"}
ref={"myModal"}
backdropColor={modalBackgroundColor}
onClosed={() => {
this.scanner.reactivate();
}}
>
<Text style={styles.text}>{modalText}</Text>
</Modal>
);
}
问题是我的onRead() 是一个异步函数,但renderModal() 在我收到响应之前呈现!
有什么解决办法吗?
提前致谢!
【问题讨论】:
标签: javascript reactjs react-native async-await