Skip to content

Instantly share code, notes, and snippets.

@gaearon
Created March 1, 2018 22:43
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gaearon/2b1cb46aff83c19a9de97f8652389424 to your computer and use it in GitHub Desktop.
Save gaearon/2b1cb46aff83c19a9de97f8652389424 to your computer and use it in GitHub Desktop.
const translationFetcher = createFetcher(async (text) => {
const res = await fetch(`${API_URL}?key=${API_KEY}&text=${text}&lang=en-ru`);
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',
};
render() {
return (
<>
<input
value={this.state.input}
onChange={e => this.setState({ input: e.target.value })}
/>
<br />
<Placeholder
delayMs={3000}
fallback={<b>translating...</b>}
>
<Translate text={this.state.input} />
</Placeholder>
</>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment