React getInitialState无法正常工作

时间:2018-03-21 20:40:19

标签: javascript html reactjs

我是React的新手,我正在尝试制作抵押贷款计算器,同时学习框架的基础知识。我的问题是,如何解决以下问题:当我更改任何输入字段时,由rate设置为getInitialState的{​​{1}}与其他所有内容一起消失,而React则不再知道2的状态。非常感谢提前!



rate

 
var MortgageCalc = React.createClass({
  mortgageCalc: function() {
    //findDOMNode to pull entered values into function   
    var principal = React.findDOMNode(this.refs.principal).value,
        rate = React.findDOMNode(this.refs.rate).value * 0.01,
        term = React.findDOMNode(this.refs.term).value * 12;
    var monthlyRate = rate/12;
    var factor = Math.pow(monthlyRate + 1, term);
    var numerator = monthlyRate * factor;
    var denominator = factor - 1;
    var quotient =  numerator/denominator;
    var payment = principal * quotient;
    console.log('$' + payment.toFixed(2));
    return payment.toFixed(2);
  },
  getInitialState: function() {
		
    return {value:{rate: 2}, payment: '0.00'}
  },
  handleChange: function(event) {
    this.setState({value: event.target.value, payment: this.mortgageCalc()});
  },
  render: function() {
    return (
      <div className="form-group">
        <h2>Mortgage Calculator</h2>
        <form className="form-group">
          <input type="number" ref="principal" placeholder="Loan Amount" value={this.state.value.principal} onChange={this.handleChange}/><br/>
          <input type="number" ref="rate" placeholder="Interest Rate" value={this.state.value.rate} onChange={this.handleChange}/><br/>
          <input type="number" ref="term" placeholder="Length of loan" value={this.state.value.term} onChange={this.handleChange}/><br/>
          <h3>Your estimated monthly payment is ${this.state.payment}</h3>
        </form>        
      </div>
    )
  }
});

ReactDOM.render(<MortgageCalc/>, document.getElementById('form'));
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

这是因为您在value方法中将handleChange保存为数字而不是对象:

handleChange: function(event) {
    this.setState({value: event.target.value, payment: this.mortgageCalc()});
}

应该是:

handleChange: function(event) {
    this.setState({
        value: { rate: event.target.value }, 
        payment: this.mortgageCalc()
    });
}

var MortgageCalc = React.createClass({
  mortgageCalc: function() {
    // PUll values from state 
    var principal = this.state.value.principal,
        rate = this.state.value.rate * 0.01,
        term = this.state.value.term * 12;
    var monthlyRate = rate/12;
    var factor = Math.pow(monthlyRate + 1, term);
    var numerator = monthlyRate * factor;
    var denominator = factor - 1;
    var quotient =  numerator/denominator;
    var payment = principal * quotient;
    return payment.toFixed(2);
  },
  getInitialState: function() {
    return {
      value:{
        rate: 2,
        principal: 30000,
        term: 2
      }, 
      payment: '0.00'
    }
  },
  componentDidMount() {
    this.setState({ payment: this.mortgageCalc() })
  },
  handleChange: function(event) {
    this.setState({
      value: { 
        ...this.state.value,
        [event.target.name]: event.target.value 
      }, 
      payment: this.mortgageCalc()
    });
  },
  render: function() {
    return (
      <div className="form-group">
        <h2>Mortgage Calculator</h2>
        <form className="form-group">
          <input type="number" name="principal" placeholder="Loan Amount" value={this.state.value.principal} onChange={this.handleChange}/><br/>
          <input type="number" name="rate" placeholder="Interest Rate" value={this.state.value.rate} onChange={this.handleChange}/><br/>
          <input type="number" name="term" placeholder="Length of loan" value={this.state.value.term} onChange={this.handleChange}/><br/>
          <h3>Your estimated monthly payment is ${this.state.payment}</h3>
        </form>        
      </div>
    )
  }
});

ReactDOM.render(<MortgageCalc/>, document.getElementById('form'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react-dom.min.js"></script>
<div class="container-fluid" id="form"></div>