我在 React Native 中遇到了功能問題。我檢查字串是電子郵件、文本還是電話并回傳適當的物件,例如 Linking.openURL( mailto:${phone}
)。
但是函式會自動觸發 URL 并將我轉發到電話應用程式,我做錯了什么?
檢查字串中的值的函式
function checkResult(result){
let emailChecker = /^(([^<>()\[\]\\.,;:\s@"] (\.[^<>()\[\]\\.,;:\s@"] )*)|(". "))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9] \.) [a-zA-Z]{2,}))$/;
if( !( emailChecker.test(result))){
return Linking.openURL(`mailto:${result}`)
}
if(isNaN(result)){
return Linking.openURL(`tel:${result}`)
}
}
我的可觸摸不透明度
<TouchableOpacity
onPress={checkResult(result)}
>
<Text
style={{
color: 'blue',
fontSize: 20,
marginTop: 10,
marginBottom: 40,
}}
>
{result}
</Text>
</TouchableOpacity>
uj5u.com熱心網友回復:
onPress={checkResult(result)}
這將導致在將函式checkResult
傳遞給組件時呼叫該函式,因此該函式將在渲染時執行。
將您的 onPress 回呼更新為() => checkResult(result)
.
<TouchableOpacity
onPress={() => checkResult(result)}
>
<Text
style={{
color: 'blue',
fontSize: 20,
marginTop: 10,
marginBottom: 40,
}}
>
{result}
</Text>
</TouchableOpacity>
有關將函式傳遞給組件的更多詳細資訊 -這里
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/470303.html
標籤:javascript 反应式 功能 可触摸不透明度
下一篇:工廠函式JS