Skip to content

Instantly share code, notes, and snippets.

@mdebbar
Last active February 26, 2018 03:54
Show Gist options
  • Select an option

  • Save mdebbar/357952f94f5b5f326b1f9d16939488c4 to your computer and use it in GitHub Desktop.

Select an option

Save mdebbar/357952f94f5b5f326b1f9d16939488c4 to your computer and use it in GitHub Desktop.
// index.js
ReactDOM.render(<App />, root);
// components/app.js
class App extends Component {
render() {
return (
<>
<h1>My App</h1>
<ConverterForm />
...
</>
);
}
}
// components/converter.js
class ConverterForm extends Component {
state = {
fromCurrency: 'USD', // put some default value here..
toCurrency: 'GBP',
amount: 100,
result: null,
};
render() {
const { fromCurrency, toCurrency, amount, result } = this.state;
return (
<form onSubmit={this.calculate}>
<input value={fromCurrency} onChange={(evt) => this.setState({ fromCurrency: evt.target.value })} />
<input value={amount} onChange={(evt) => this.setState({ amount: evt.target.value })} />
<input value={toCurrency} onChange={(evt) => this.setState({ toCurrency: evt.target.value })} />
{result !== null
? <p>Result: {result}</p>
: null
}
</form>
);
}
calculate = () => {
const { fromCurrency, toCurrency, amount } = this.state;
ConversionUtils.convert(fromCurrency, toCurrency, amount).then((result) => {
this.setState({ result });
});
}
}
// utils/conversion.js
const ConversionUtils = {
convert(fromCurrency, toCurrency, amount) {
// Make an API call to get the conversion result.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment