0

In the case of this code, I have to click the calculate button twice to find answers of BMI and BMI judge. How to code it to click only once find the two answers?

bmiCalc (){ this.setState({bmi:this.state.weight/(this.state.height*this.state.height)*10000}); if(this.state.bmi<18.5){this.setState({judge: 'Underweight'});} else if(this.state.bmi<24.9){this.setState({judge:'Normal weight'});} else if(this.state.bmi<30){this.setState({judge:'Overweight'});} else{this.setState({judge:'Obesity'});} } render(){ return ( <ul><button onClick={this.bmiCalc.bind(this)}>calculate</button></ul> <ul>BMI: {Math.round(this.state.bmi*10)/10}</ul> <ul>BMI judge :{this.state.judge} </ul> ); } 
3
  • Please put your code in your question. Or use links at least!!! Commented Nov 9, 2015 at 16:18
  • ps, BMI is a bunch of bullsh!t. Commented Nov 9, 2015 at 16:31
  • Your JSX syntax is invalid. render() returns multiple elements, they're not an array, not even separated by commas. Not to mention that React forbids render() from returning multiple root elements. Commented Nov 9, 2015 at 17:38

2 Answers 2

1

You could use something like:

function desiredActions (){ action1() action2() } <button onclick={desiredActions}></button> 

Basically just wrap the functions you want to call in a function and assign it to the onclick handler.

Note: this isn't specific to React or ES6, just basic JavaScript.

Sign up to request clarification or add additional context in comments.

Comments

0

You can do this by calling the set state once at the end of your function:

bmiCalc() { const bmi = this.state.weight/(this.state.height*this.state.height)*10000; let judge = 'Obesity'; if(this.state.bmi<18.5){this.setState({judge: 'Underweight'});} else if(this.state.bmi<24.9){this.setState({judge:'Normal weight'});} else if(this.state.bmi<30){this.setState({judge:'Overweight'});} this.setState({bmi: bmi, judge: judge}); } 

1 Comment

Thank you. Your answer is of some help. codehere

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.