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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

超過ReactJs componentDidMount 最大更新深度

超過ReactJs componentDidMount 最大更新深度

斯蒂芬大帝 2023-10-20 17:30:10
我理解這個(gè)錯(cuò)誤,我的 componentDidUpdate 函數(shù)正在創(chuàng)建一個(gè)無限循環(huán),我不知道如何修復(fù)它。我發(fā)現(xiàn)回溯顯示了兩個(gè)錯(cuò)誤,但我不知道該怎么辦。這是提交處理函數(shù),它位于主(日志)組件中:submitHandler = event => {              event.preventDefault();    const {month, day, year} = this.state.data;    this.setState({       loading: true,       error: false,       errors: {},       data: {          ...this.state.data,          foundation: `${month} ${day} de ${year}`       }    });    fetch('http://127.0.0.1:8000/logup/',       {          method: 'post',          headers: {'Content-Type': 'application/json'},          body: JSON.stringify(this.state.data)        }).then(response => {            this.setState({ loading: false });            if (response.ok) {                console.log('redirect to social nets');            } else {                this.setState({ error: true });            }            return response.json();        }).then(json => {            if (this.state.error) {                this.setState({errors: json}) // The traceback give me error right here            } else {                console.log(json);            }        });        };我在該登錄組件的渲染中也有許多輸入組件,回溯也在這里顯示錯(cuò)誤。state = {    error: false};componentDidUpdate() {      let bad = Object.keys(this.context.errors).includes(this.props.name);      if (bad) {         this.setState({ error: true }); // traceback give me error too.              };   };
查看完整描述

3 回答

?
30秒到達(dá)戰(zhàn)場(chǎng)

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊

在您的componentDidUpdate狀態(tài)中,按以下方式更新:


this.setState({ error: true })

您這樣做的條件是:


Object.keys(this.context.errors).includes(this.props.name)

設(shè)置狀態(tài)會(huì)導(dǎo)致組件重新渲染并更新,因此componentDidUpdate將再次運(yùn)行。然而,當(dāng)這種情況發(fā)生時(shí),您的上述情況很可能仍然成立。因此,你將再次更新狀態(tài),React 不會(huì)短路狀態(tài)更新,因?yàn)槟忝看味紕?chuàng)建一個(gè)新對(duì)象。在這里拯救自己的一種簡(jiǎn)單方法是將您的條件更改為:


      if (bad && !this.state.error) {

         this.setState({ error: true }); // traceback give me error too.        

      };


查看完整回答
反對(duì) 回復(fù) 2023-10-20
?
青春有我

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超8個(gè)贊

您應(yīng)該在內(nèi)部使用某種類型的條件componentDidUpdate,以免觸發(fā)狀態(tài)更新。就像下面這樣:


componentDidUpdate(prevProps, prevState) {

      let bad = Object.keys(this.context.errors).includes(this.props.name);

      if (prevState.error !== this.state.error && bad) {

         this.setState({ error: true }); // traceback give me error too.        

      };

   };

始終比較內(nèi)部的 prevState 和 currentState 值,componentDidUpdate因?yàn)樵诖蠖鄶?shù)情況下這個(gè)條件就足夠了。


注意:開始使用之前,componentDidUpdate請(qǐng)參閱文檔,因?yàn)樗€提供了prevProps在prevState這種情況下始終有用的值。


查看完整回答
反對(duì) 回復(fù) 2023-10-20
?
婷婷同學(xué)_

TA貢獻(xiàn)1844條經(jīng)驗(yàn) 獲得超8個(gè)贊

改變


if (bad) {

    this.setState({ error: true }); // traceback give me error too.        

};


if (bad && !this.state.error) {

    this.setState({ error: true }); // traceback give me error too.        

};

setState在 of 內(nèi)部使用componentDidUpdate將導(dǎo)致它重新渲染和調(diào)用componentDidUpdate。添加額外的檢查將有助于停止此循環(huán)。


查看完整回答
反對(duì) 回復(fù) 2023-10-20
  • 3 回答
  • 0 關(guān)注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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