我是 React Native 的新手。我的應用程式中有一個文本輸入區域。我想將用戶寫的文本發送到我的 gmail 。我怎樣才能做到這一點?
這是我的代碼:
import React, { useState } from "react"
import { View, Text, TextInput } from "react-native"
import ModalButton from "./ModalButton"
import styles from "./theme"
const StoreReview = (props) => {
const [comment, setComment] = useState('')
return (
<View style={styles.container}>
<Text style={styles.headerText}>
We are very sorry about this.
Can you share your experiences?
</Text>
<View style={styles.commentArea}>
<TextInput
onChangeText={setComment}
value={comment}
multiline={true}
/>
</View>
<View style={styles.button}>
<ModalButton
buttonText={'Send'}
onPress={() => {
console.log(comment, 'send that commet to gmail')
}}
/>
</View>
</View>
)
}
export default StoreReview
uj5u.com熱心網友回復:
您可以使用帶有方案的鏈接模塊mailto來打開郵件應用程式。
匯入Linking模塊:
import { Linking } from 'react-native';
然后,使用其openURL()功能打開郵件應用程式:
await Linking.openURL('mailto:[email protected]');
您還可以使用該canOpenURL()函式來檢查鏈接是否受支持:
const url = 'mailto:[email protected]';
const supported = await Linking.canOpenURL(url);
if (supported) {
await Linking.openURL(url);
}
所以,在你的例子中,這看起來像:
import React, { useState, useCallback } from 'react';
import { View, Text, TextInput, Linking } from 'react-native';
import ModalButton from './ModalButton';
const StoreReview = (props) => {
const [comment, setComment] = useState('');
const emailAddress = '[email protected]';
const url = `mailto:${emailAddress}?body=${comment}`;
const handlePress = useCallback(async () => {
const supported = await Linking.canOpenURL(url);
if (supported) {
await Linking.openURL(url);
}
}, [url]);
return (
<View style={styles.container}>
<Text style={styles.headerText}>
We are very sorry about this.
Can you share your experiences?
</Text>
<View style={styles.commentArea}>
<TextInput
onChangeText={setComment}
value={comment}
multiline={true}
/>
</View>
<View style={styles.button}>
<ModalButton
buttonText="Send"
onPress={handlePress}
/>
</View>
</View>
);
}
export default StoreReview;
編輯:我剛剛意識到您想comment在電子郵件正文中包含 。
要設定電子郵件主題和正文,只需將您的 URL 從 更改mailto:[email protected]為mailto:[email protected]?subject=Hello&body=World。
所以,在你的情況下,URL 應該是:
const [comment, setComment] = useState('');
const emailAddress = '[email protected]';
const url = `mailto:${emailAddress}?body=${comment}`;
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/312931.html
標籤:javascript 反应原生 电子邮件
下一篇:了解家庭計算機基礎網路配置
