0

What I am trying to achieve is to have an input box's value set once a percentage is selected. But to calculate that value I need the value of the purchasePrice. I've tried accessing the purchasePrice property in the function of onDownPaymentPercentChange but that didn't work. How I do accomplish this?

export default class PurchaseInfo extends Component{
constructor(props){
    super(props);
    this.state ={
        purchasePrice:'',
        downPayment:'',
    }
}

onPurchasePriceChange = (e) => {
        this.setState({
            purchasePrice: e.target.value
        })
    }

onDownPaymentPercentChange = (e) => {
        console.log(e.target.value)
        this.setState({
            downPayment: e.target.value * this.purchasePrice
        })
    }
render(){
  return(
  <div>
     <label>Purchase Price</label>
     <br/>
     <input id='PurchasePrice' required placeholder='$' onChange={this.onPurchasePriceChange}></input>
     <br/>
      <br/>
     <label>Percentage of purchase price</label>      
     <br/>
     <select onChange={this.onDownPaymentPercentChange}>
                    <option value='.035'>3.5%</option>
                    <option value='.05'>.5%</option>
     </select>
     <br/>
     <br>
    <label> Down Payment <label />
      <br/>
     <input required placeholder='$' value={this.state.downPayment}> 
     </input>
 </div>

2 Answers 2

2

You are trying to access a class property called purchasePrice instead of a state value.

onDownPaymentPercentChange = (e) => {
  this.setState({
    downPayment: e.target.value * this.purchasePrice // Problem is here
  })
}

Access purchasePrice through the state object like this: this.state.purchasePrice.

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

Comments

0

You a not accessing correctly your state. Also, check the purchasedPrice state beforehand

onDownPaymentPercentChange = (e) => {
   console.log(e.target.value);
   if(this.state.purchasePrice === ''){
      return;
   }
   this.setState({
        downPayment: e.target.value * this.state.purchasePrice
   });
}

2 Comments

You don't need to bind arrow functions.
It's always something simple. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.