Skip to content

Instantly share code, notes, and snippets.

@Veerse
Created August 9, 2019 10:23
Show Gist options
  • Save Veerse/3bf835c3d8b7412aaf668137e237237c to your computer and use it in GitHub Desktop.
Save Veerse/3bf835c3d8b7412aaf668137e237237c to your computer and use it in GitHub Desktop.
IMC Calculator
<div id="root">
<!-- This div's content will be managed by React. -->
</div>
function OutputIMC(props){
return (
<div>
{(props.kg/Math.pow((props.cm)/100,2)).toFixed(2)}
</div>
)
}
class InputKG extends React.Component {
constructor(props){
super(props);
}
onChange(e) {
this.props.onWeightChange(e.target.value);
}
render(){
return(
<input type="text" value= {this.props.Weight} onChange={this.onChange.bind(this)} />
)
}
}
class InputCM extends React.Component {
constructor(props){
super(props);
}
onChange(e){
this.props.onHeightChange(e.target.value);
}
render(){
return(
<input type="text" value = {this.props.Height} onChange={this.onChange.bind(this)} />
)
}
}
class Inputs extends React.Component {
constructor(props){
super(props);
}
handleWeightChange(val){
this.props.onWeightChange(val);
}
handleHeightChange(val){
this.props.onHeightChange(val);
}
render () {
return(
<div>
kg:<InputKG Weight={this.props.kg} onWeightChange={this.handleWeightChange.bind(this)}/>
cm:<InputCM Height={this.props.cm} onHeightChange={this.handleHeightChange.bind(this)}/>
</div>
)
}
}
class App extends React.Component {
constructor(props){
super(props);
this.state = ({kg: 80, cm: 185});
}
handleWeightChange(val){
this.setState({kg: val});
}
handleHeightChange(val){
this.setState({cm: val});
}
render(){
return (
<div>
<Inputs
kg={this.state.kg}
cm={this.state.cm}
onWeightChange={this.handleWeightChange.bind(this)}
onHeightChange={this.handleHeightChange.bind(this)} />
<OutputIMC
kg={this.state.kg}
cm={this.state.cm}
/>
</div>
)
}
}
ReactDOM.render(
<App />,
document.getElementById('root')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react-dom.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment