简体   繁体   English

React函数抵押计算器返回NAN

[英]React function mortgage calculator returns NAN

The function handleCostChange is supposed to calculate the mortgage payment based on the inputs. 函数handleCostChange应该基于输入来计算抵押付款。 It currently returns NAN- I first wrote it in JavaScript and it worked well. 目前它返回NAN-我首先用JavaScript编写了它,并且效果很好。 I'm trying to figure out what went wrong when I tried to refactor this function from Javascript and add it to my React component. 我试图找出从Javascript重构此函数并将其添加到我的React组件时出了什么问题。

import React, { Component } from 'react';

import Counter from './components/Counter';
import './App.css';

class App extends Component {
  state = {
    cost: 0,
    houseCost: 0,
    downPayment: 0,
    termOfLoan: 0,
    annualInterestRate: 0
  };
  handleChange = (e) => {
    this.setState({
      houseCost: e.target.houseCost,
      downPayment: e.target.downPayment,
      termOfLoan: e.target.termOfLoan,
      annualInterestRate: e.target.annualInterestRate 
    });
  };
  handleCostChange = () => {
    const principal = this.state.houseCost - this.state.downPayment
    const percentageRate = this.state.annualInterestRate / 1200
    const lengthOfLoan = 12 * this.state.termOfLoan
    this.setState(
      prevState => ({
        cost: (prevState.cost = (principal * percentageRate) / (1 - (Math.pow((1 + percentageRate) , lengthOfLoan * -1)))).toString()
      });
    );
  };
  render() {
    return (
      <div className="App">
        <Counter
          cost={this.state.cost}
          houseCost={this.state.houseCost}
          downPayment={this.state.downPayment}
          termOfLoan={this.state.termOfLoan}
          annualInterestRate={this.state.annualInterestRate}
          changeCost={this.handleCostChange}
          handleChange={this.handleChange}
        />
      </div>
    );
  }
}

export default App;

Counter.js file Counter.js文件

import React from 'react';


const Counter = (props) => {

        return (
            <div className="counter">
                <input 
                   type="number" 
                   placeholder="House Cost" 
                   onChange={(e) => props.handleChange(e)}>
                </input>
                <input 
                   type="number" 
                   placeholder="Down Payment" 
                   onChange={(e) => props.handleChange(e)}>
                </input>
                <input 
                   type="number" 
                   placeholder="Mortgage Period (years)" 
                   onChange={(e) => props.handleChange(e)}>
                </input>
                <input 
                   type="number" 
                   placeholder="Interest Rate" 
                   onChange={(e) => props.handleChange(e)}>
                </input>
                <button 
                   className="counter-action" 
                   onClick={props.changeCost}
                >Calculate
                </button>
                <span className="counter-score">{ props.cost }</span>
            </div>
            );
    }


export default Counter;

Try this 尝试这个

  handleCostChange = () => {
       const { houseCost, downPayment, annualInterestRate, termOfLoan } = this.state;
       const principal = houseCost - downPayment
       const percentageRate = annualInterestRate / 1200
       const lengthOfLoan = 12 * termOfLoan;
       const cost = (principal * percentageRate) / (1 - (Math.pow((1 + percentageRate) , lengthOfLoan * -1))).toString();
       this.setState({
           cost
       })
   }

Also you need further changes. 您还需要进一步的更改。 Create individual event handlers and set the value using event.target.value 创建单个事件处理程序并使用event.target.value设置值

   handleCostChange = (e) => {
        this.setState({
            houseCost: e.target.value,
        });
   }

   handleDownPayment = (e) => {
        this.setState({
            downPayment: e.target.value,
        });
   }

   handleannualInterestRate = (e) => {
        this.setState({
           annualInterestRate : e.target.value,
        });
   }

   handleTermOfLoan = (e) => {
        this.setState({
            termOfLoan: e.target.value,
        });
   }

And pass down all event handlers 并传递所有事件处理程序

       <Counter
      cost={this.state.cost}
      houseCost={this.state.houseCost}
      downPayment={this.state.downPayment}
      termOfLoan={this.state.termOfLoan}
      annualInterestRate={this.state.annualInterestRate}
      handleCostChange={this.handleCostChange}
     handleTermOfLoan ={this.handleTermOfLoan}
     handleannualInterestRate={this.handleannualInterestRate}
     handleDownPayment={this.handleDownPayment}
    />

Now in Counter.js pass values as value prop to input elements along with individual event handler functions 现在在Counter.js中,将值作为值的属性传递给输入元素以及各个事件处理函数

        <input type="number" placeholder="House Cost" onChange={(e) => props.handleCostChange(e)} value={props.houseCost}></input>
            <input type="number" placeholder="Down Payment" onChange={(e) => props.handleDownPayment(e)} value={props.downPayment}></input>
            <input type="number" placeholder="Mortgage Period (years)" onChange={(e) => props.handleTermOfLoan(e)} value={props.termOfLoan}></input>
            <input type="number" placeholder="Interest Rate" onChange={(e) => props.handleannualInterestRate(e)} value={annualInterestRate}></input>

Please excuse me for typo errors because I am answering on my mobile 请原谅我输入错误,因为我正在用手机接听电话

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM