Easy React Hooks translator
- Input
- Debouncing
- Dropdown
Easy React Hooks translator
import React, { useState, useEffect } from 'react'; | |
import axios from 'axios'; | |
const Convert = ({ language, text }) => { | |
const [translated, setTranslated] = useState(''); | |
const [debouncedText, setDebouncedText] = useState(text); | |
useEffect(() => { | |
const timerId = setTimeout(() => { | |
setDebouncedText(text); | |
}, 500); | |
return () => { | |
clearTimeout(timerId); | |
}; | |
}, [text]); | |
useEffect(() => { | |
const doTranslation = async () => { | |
const { data } = await axios.post( | |
'https://translation.googleapis.com/language/translate/v2', | |
{}, | |
{ | |
params: { | |
q: debouncedText, | |
target: language.value, | |
key: API_KEY, | |
}, | |
} | |
); | |
setTranslated(data.data.translations[0].translatedText); | |
}; | |
doTranslation(); | |
}, [language, debouncedText]); | |
return ( | |
<div> | |
<h1 className="ui header">{translated}</h1> | |
</div> | |
); | |
}; | |
export default Convert; |
import React, { useState, useEffect, useRef } from 'react'; | |
const Dropdown = ({ label, options, selected, onSelectedChange }) => { | |
const [open, setOpen] = useState(false); | |
const ref = useRef(); | |
useEffect(() => { | |
const onBodyClick = (event) => { | |
if (ref.current.contains(event.target)) { | |
return; | |
} | |
setOpen(false); | |
}; | |
document.body.addEventListener('click', onBodyClick); | |
return () => { | |
document.body.removeEventListener('click', onBodyClick); | |
}; | |
}, []); | |
const renderedOptions = options.map((option) => { | |
if (option.value === selected.value) { | |
return null; | |
} | |
return ( | |
<div | |
key={option.value} | |
className="item" | |
onClick={() => onSelectedChange(option)} | |
> | |
{option.label} | |
</div> | |
); | |
}); | |
return ( | |
<div ref={ref} className="ui form"> | |
<div className="field"> | |
<label className="label">{label}</label> | |
<div | |
onClick={() => setOpen(!open)} | |
className={`ui selection dropdown ${open ? 'visible active' : ''}`} | |
> | |
<i className="dropdown icon"></i> | |
<div className="text">{selected.label}</div> | |
<div className={`menu ${open ? 'visible transition' : ''}`}> | |
{renderedOptions} | |
</div> | |
</div> | |
</div> | |
</div> | |
); | |
}; | |
export default Dropdown; |
import React, { useState } from 'react'; | |
import Translate from './components/Translate'; | |
export default () => { | |
return ( | |
<div> | |
<Translate /> | |
</div> | |
); | |
}; |
import React, { useState } from 'react'; | |
import Dropdown from './Dropdown'; | |
import Convert from './Convert'; | |
const options = [ | |
{ | |
label: 'Afrikaans', | |
value: 'af', | |
}, | |
{ | |
label: 'Arabic', | |
value: 'ar', | |
}, | |
{ | |
label: 'Hindi', | |
value: 'hi', | |
}, | |
{ | |
label: 'Dutch', | |
value: 'nl', | |
}, | |
]; | |
const Translate = () => { | |
const [language, setLanguage] = useState(options[0]); | |
const [text, setText] = useState(''); | |
return ( | |
<div> | |
<div className="ui form"> | |
<div className="field"> | |
<label>Enter Text</label> | |
<input value={text} onChange={(e) => setText(e.target.value)} /> | |
</div> | |
</div> | |
<Dropdown | |
label="Select a Language" | |
selected={language} | |
onSelectedChange={setLanguage} | |
options={options} | |
/> | |
<hr /> | |
<h3 className="ui header">Output</h3> | |
<Convert text={text} language={language} /> | |
</div> | |
); | |
}; | |
export default Translate; |