I'm developing a project, and when typing a value in one field, I need the other field to be changed based on the values I defined in a function, here is the code:
constructor(props) {
super(props);
this.state = {
yieldMonth: '',
risk: '',
}
}
changeYieldMonthHandler= (event) => {
this.setState({yieldMonth: event.target.value});
}
changeRiskHandler= (event) => {
this.setState({risk: event.target.value});
}
In the function below, I check the amount that will be inserted in the Monthly Income field, which should impact the risk field:
definesRisk(){
if (this.state.yieldMonth != null){
if (this.state.yieldMonth > 6000){
this.state.risk= "A";
} else if (this.state.yieldMonth > 1000 && this.state.yieldMonth <= 8000){
this.state.risk = "B";
} else {
this.state.risk = "C";
}
}
}
And here is my render, where it should be updated dynamically according to the value of the field Monthly income. I'm trying to do it that way, does anyone help me know where I'm going wrong?
render() {
return (
<div>
<div className="form-group">
<label> Yield Month: </label>
<input type ="Number" placeholder="Yield Month:" name="yieldMonth" className="form-control"
value={this.state.yieldMonth} onChange={this.changeYieldMonthHandler} onBlur={this.definesRisk} />
</div>
<div className="form-group">
<label> Risk: </label>
<input disabled type ="Number" placeholder="" name="risk" className="form-control"
value={this.state.risk} onChange={this.definesRisk()} />
</div>
</form>
</div>
</div>
</div>
</div>
)
}
this.state.risk= "A";. You should always use setState to change the state.this.setState({risk: 'A'})