Trying to get the BMR to show without reloading the page

Multi tool use
Multi tool use


Trying to get the BMR to show without reloading the page



Below is my code and I got a feeling I am really doing this wrong. I am new to react and I been spending many hours trying to figure this out with no luck.



I am trying to get users to input values of age, gender, height, weight etc..and then make the BMR box update with a value.



What i have so far is when the user clicks "Calculate for BMR" the onClick function spits out the correct result, but I have no clue how to get the value to appear in the "BMR input box" without any sort of refreshing.



Any help would be appreciated. Thanks


class Calculator extends React.Component {
constructor(props) {
super(props);
this.state = {};

this.handleGenderChange = this.handleGenderChange.bind(this);
this.handleAgeChanged = this.handleAgeChanged.bind(this);
this.handleWeightChanged = this.handleWeightChanged.bind(this);
this.handleFeetChanged= this.handleFeetChanged.bind(this);
this.handleInchesChanged=this.handleInchesChanged.bind(this);


}

handleGenderChange = (event) => {
this.setState({Gender: event.target.value});
}

handleAgeChanged = (event) => {
this.setState({Age: event.target.value});
}

handleWeightChanged = (event) => {
this.setState({Weight: event.target.value});
}

handleHeightChanged = (event) => {
this.setState({Height: event.target.value});
}

handleFeetChanged = (event) => {
this.setState({Feet: event.target.value});
}
handleInchesChanged = (event) => {
this.setState({Inches: event.target.value});
}
onClick= (event) => {
event.preventDefault();
console.log(this.state);

const totalHeight = Number(this.state.Feet) * 12 + Number(this.state.Inches);

if(this.state.Gender == 'Male'){
var BMR = 66 + (6.23 * Number(this.state.Weight)) + (12.7 * totalHeight) - (6.8 * Number(this.state.Age));
console.log(BMR);
}

if(this.state.Gender == 'female'){
var BMR = 655 + (4.35 * Number(this.state.weight)) + (4.7 * totalHeight) - (4.7 * Number(this.state.age));
console.log(BMR);
}

}

render() {
return (





Calories/TDEE Calculator





-- Gender--
Male
Female




Age




Weight (lbs)




"Height (Ft/In)"






BMR


<button className="btn btn-lg btn-primary btn-block" onClick={this.onClick.bind(this)}>Click for BMR</button> <br />

&nbsp; &nbsp;

</form>

</Container>

</div>


);
}

}
export default Calculator;



EDIT:
Thanks everyone for taking your time to help, it worked :D. I learned from all your replies.




3 Answers
3



Just set a BMRvalue inside your state and link that to the input value.


state = {
BMRvalue: ''
}



Then add this to your onClick function:


this.setState({
BMRvalue: BMR
})



And slightly change your rendered input:



BMR






note that { BMRvalue = ''} is not proper syntax at all, with errors on both the initialisation as well as that setState you show.
– Mike 'Pomax' Kamermans
24 mins ago



{ BMRvalue = ''}





That was really ugly. I've edited those, thanks.
– Felipe Lanza
20 mins ago



You don't have anything that renders this.state.BMR so it's not too surprising you don't see it anywhere. Some advice: don't use nonbreaking spaces and <br>: that's what CSS is for. Also, don't use bind, use arrow notation to preserve this, there is no reason to use all these bind calls in your constructor.


this.state.BMR


<br>


this


bind



And then for the actual question: you need to actually render something, so have an element that either shows the button, or shows the BMR value, based on whether you computed it:


class Calculator extends React.Component {
constructor(props) {
super(props);
this.state = {};
}

handleGenderChange(evt) {
// you really should validate this
this.setState({Gender: event.target.value});
}

...

render() {
return


...
this.handleInchesChanged(evt) } />
...
{ this.showButtonOrResult() }
;
}

showButtonOrResult() {
// if the button wasn't clicked yet, then `BMR` will not yet be a state value
if (!this.state.BMR) {
return <button className="..." onClick={evt => this.onClick(evt)>Click for BMR</button>
}
// if it IS a state value, just render it
return
Your BMR is: { this.state.BMR }
;
}
}



So when your button is clicked, you do what you do, calculate BMR, then setState that, and render() automatically gets called again. Now there is a value to show, and instead of the button, it'll show a div with the result.


setState


render()



Also note that we are absolutely note using bind(this) in the constructor, because that's ridiculous. Properly handle your events with an arrow function so that you get the event, and then pass the event to the correct function, with normal this context.


bind(this)


this



You need to label your state properties correctly. Your female calculation is going to spit out NaN because you're using {this.state.weight} when you're setting it as 'Weight'



Initialize your state


this.state = {
bmr: ''
};



Set the value of your input



BMR




Set the state in your onclick function


onClick = (event) => {
event.preventDefault();

let BMR;

const totalHeight = Number(this.state.Feet) * 12 + Number(this.state.Inches);

if (this.state.Gender === 'Male') {
BMR = 66 + (6.23 * Number(this.state.Weight)) + (12.7 * totalHeight) - (6.8 * Number(this.state.Age));
this.setState({ bmr: BMR });
} else if (this.state.Gender === 'Female') {
BMR = 655 + (4.35 * Number(this.state.Weight)) + (4.7 * totalHeight) - (4.7 * Number(this.state.Age));
this.setState({bmr: BMR});
}
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

gQx2AjRgABE,uMiR IVmdh 7tD,lXNCTLA4G3ld0sGmTvVe8zzHH,JOKE7lVMY280jxhHOKx9Ncb,jE6zc,n16zXmjUW
Jv SU9i,av J,lE3ZeZP1lAg7 dXsLqIx6d3Ty8 fyY2Sv,o tx,DzJI0jVoV,nVb3 rdyVbBGM48um Je4FHbYszYKTbhhZJLLT p

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications