Never ever setState inside render, there is a much better way to do what you are trying to do,
I take it you want the price change happens when the billing and the planType are set to a specific values, to do this all you need to do is check and setState the price when and where these two values change!
For example you are saying these two value are getting populated through componentDidMount, so you simply need to check their value in that place and set the price there!, its as simple as that.
Here is some code:
import React, { PureComponent } from 'react';
class MyClass extends PureComponent {
state = {
billing: null,
planType: null,
total_invoice: 0,
}
componentDidMount() {
//These can change through api or any other way you want!
const newBillingType = 'Monthly';
const newPlanType = 'Start-Up Plan';
this.setState({ billing: newBillingType, planType: newPlanType })
if (newBillingType === "Monthly" && newPlanType === "Start-Up Plan") {
this.setState({ price: this.state.total_invoice + 35 })
}
}
render() {
return <div>You will be charged {this.state.price}</div>;
}
}
export default MyClass;
Please note that if you call setState inside a function multiple times (without any timeout) like i did in componentDidMount, because the setState doesn't happen instantly, React will combine the result and only does one setState which is a good thing and avoids multiple updates.