[英]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.