[英]Conditional rendering in a promise in react native
我正在从外部 api 获取数据。 此数据是货币对,例如 EURUSD,分别具有实时卖价和买价,例如 1.17123 , 1.17150。 因此,应用程序的用户需要输入未来价格,以便如果要价或买价达到用户输入的价格,则会记录一条消息。 我尝试使用承诺,但它会立即输出用户在 textinput 中输入的价格,我运行应用程序而不是检查价格是否达到了用户期望在达到时收到提醒的未来价格。 下面是我的代码:
const [pricealert, setPricealert] = useState(0)
function checkAlertCondition (){
return new Promise((resolve, reject) => {
if(pricealert >= {...data.prices.AskPrice})
{
resolve({
Pair: {...data.prices.instrument},
message: "Price" + pricealert + "Has been hit"
});
} else if (pricealert <= {...data.prices.BidPrice}) {
resolve({
Pair:{...data.prices.instrument},
message: "Price" + pricealert + "has been hit"
});
} else {
reject ("Create Alert")
}
});
}
<Modal visible={modalopen} animationType={"fade"}>
<View style={styles.modal}>
<View>
<Text style={{textAlign: "center", fontWeight: "bold"}}>
{data.prices[clickedindex].instrument}
</Text>
<Text style={{textAlign: "center"}}>
{data.prices.AskPrice}/{data.prices.BidPrice}
</Text>
<Card.Divider/>
<View style={{ flexDirection: "row"}}>
<View style={styles.inputWrap}>
<TextInput
style={styles.textInputStyle}
value={pricealert}
onChangeText = {(pricealert) => setPricealert(pricealert)}
placeholder="Alert Price"
placeholderTextColor="#60605e"
numeric
keyboardType='decimal-pad'
/>
</View>
</View>
<TouchableOpacity
style={styles.button}
onPress={() => {
if(pricealert.length < 7) {
Alert.alert("Error", "Enter a valid price")
return;
} else if (pricealert.length > 7) {
Alert.alert("Error", "Enter a valid price")
return;
}
setModalOpen(false);
checkAlertCondition()
.then((message) => {
console.log(JSON.stringify(message) )
.catch((err) => {
console.log(err)
})
});} }
>
<Text style={styles.buttonTitle}>OK</Text>
</TouchableOpacity>
</View>
</View>
</Modal>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.