第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

試圖設(shè)置字段的值但它不起作用

試圖設(shè)置字段的值但它不起作用

有只小跳蛙 2021-11-18 16:31:02
這是我的前端我得到的變量的值interestRate,并monthlyPayment從API。我只想在前端設(shè)置這些值。這是我的代碼:class Display extends Component {componentDidMount() {    this.calculateAPR();  }  componentDidUpdate(prevProps) {      this.calculateAPR();    }  calculateAPR = () => {    let x = JSON.parse(localStorage.getItem('amount'));     a=x[0].amount;      t=x[0].years;    fetch("https://herokuapp.com/interest?amount="+a+"&numMonths="+t)      .then(res => res.json())      .then(        (result) => {          //console.log(result);          interestRate = result.interestRate;          monthlyPayment = result.monthlyPayment.amount;          console.log(interestRate, monthlyPayment);        },      )        this.calculateMonthlyRepayment(monthlyPayment);        this.percentageAPR(interestRate);  };  calculateMonthlyRepayment = (z) => {    return <p>${z}</p>;  };  percentageAPR = (z) => {    return <p>{z * 100}%</p>;  };  render() {    return (      <div className="flex">        <DisplayChild func={this.percentageAPR()} text="interest rate" />        <DisplayChild          func={this.calculateMonthlyRepayment()}          text=" monthly repayment"        />      </div>    );  }}這是我顯示這些值的地方,但這些值沒有顯示:const DisplayChild = ({ func, text }) => {  return (    <span>      {func} <small>{text}</small>    </span>  );};
查看完整描述

1 回答

?
莫回?zé)o

TA貢獻(xiàn)1865條經(jīng)驗 獲得超7個贊

您需要將值存儲在狀態(tài)信息中。實際上,您只是將它們傳遞給立即返回然后被丟棄的元素的函數(shù)。(你也沒有fetch正確處理錯誤。你不是唯一一個,fetchAPI 有一個設(shè)計缺陷,鼓勵這種腳步,我寫在這里。)

更多關(guān)于在文檔中處理狀態(tài)的信息。

看評論:

class Display extends Component {

  constructor(props) { // *** Constructor with initial state

      super(props);

      this.state = {

        interestRate: 0,  // *** Use appropriate initial values, 0 probably isn't the right choice

        monthlyPayment: 0

      });

  }


  // *** SOMETHING needs to call this function. You might do it from componentDidMount, for instance.

  calculateAPR = () => {

    let x = JSON.parse(localStorage.getItem('amount'));

    a=x[0].amount; 

    t=x[0].years;


    fetch("https://ftl-frontend-test.herokuapp.com/interest?amount="+a+"&numMonths="+t)

      .then(res => {                                   //

        if (!res.ok) {                                 // *** Note the necessary error handling

          throw new Error("HTTP error " + res.status); //

        }                                              //

        return res.json();

      })

      .then(

        (result) => {

          this.setState({

            interestRate: result.interestRate,

            monthlyPayment: result.monthlyPayment.amount

          });

        },

      )

      .catch(err => {

        // *** Handle/display error here

      });

  };


  // *** You can have these as functions if you want, but since they're pure functions

  // it A) Isn't necessary to re-create them for every instance like this, and B) Is

  // entirely possible for them to be `static` (or even outside the component and closed

  // over).

  calculateMonthlyRepayment = (z) => {

    return <p>${z}</p>;

  };


  percentageAPR = (z) => {

    return <p>{z * 100}%</p>;

  };


  render() {

    // *** You may want logic here to render differently when you don't have the data yet

    return (

      <div className="flex">

        <DisplayChild func={this.percentageAPR()} text="interest rate" />

        <DisplayChild

          func={this.calculateMonthlyRepayment()}

          text=" monthly repayment"

        />

      </div>

    );

  }

}


查看完整回答
反對 回復(fù) 2021-11-18
  • 1 回答
  • 0 關(guān)注
  • 197 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號