Last active
February 26, 2018 03:54
-
-
Save mdebbar/357952f94f5b5f326b1f9d16939488c4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // 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