📅  最后修改于: 2022-03-11 15:03:40.843000             🧑  作者: Mango
import React, { useEffect, useState } from 'react';
import axios from 'axios';
const Convert = ({ text, language }) => {
const [convertedText, setConvertedText] = useState('');
useEffect(() => {
const response = axios
.post(
'https://translation.googleapis.com/language/translate/v2',
{},
{
params: {
q: text,
target: language,
key: 'AIzaSyCHUCmpR7cT_yDFHC98CZJy2LTms-IwDlM'
}
}
)
.then((response) => {
setConvertedText(response.data.data.translations[0].translatedText);
})
.catch((err) => {
console.log('rest api error', err);
});
}, [text, language]);
return {convertedText};
};
export default Convert;