-
-
Save gaearon/376cfa2776d59cae8bc6d28402f1fd3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const translationFetcher = createFetcher(async (text) => { | |
const res = await fetch(`${API_URL}?key=${API_KEY}&text=${text}&lang=en-ru`); | |
await new Promise(resolve => setTimeout(resolve, 1000)); | |
return (await res.json()).text; | |
}); | |
function Translate(props) { | |
if (!props.text) { | |
return null; | |
} | |
return ( | |
<b>{translationFetcher.read(props.text)}</b> | |
); | |
} | |
export default class App extends PureComponent { | |
state = { | |
input: 'hello', | |
readyInput: '' | |
}; | |
render() { | |
return ( | |
<> | |
<input | |
value={this.state.input} | |
onChange={e => { | |
this.setState({ input: e.target.value }); | |
this.deferSetState({ readyInput: e.target.value }); | |
}} | |
/> | |
<br /> | |
<Placeholder | |
delayMs={3000} | |
fallback={<b>translating...</b>} | |
> | |
<Translate text={this.state.readyInput} /> | |
</Placeholder> | |
</> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment