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

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

在空的輸入字段ReactJS上顯示錯(cuò)誤

在空的輸入字段ReactJS上顯示錯(cuò)誤

慕妹3146593 2021-04-17 18:15:13
我正在嘗試基于兩個(gè)文本輸入字段中是否都存在任何字符來啟用/禁用表單按鈕,但是由于某種原因,狀態(tài)長度在我的條件下呈現(xiàn)了錯(cuò)誤,盡管我登錄狀態(tài)時(shí)卻顯示了錯(cuò)誤。錯(cuò)誤:const isEnabled = this.state.subject.length > 0 && this.state.emails.length > 0; //Uncaught TypeError: Cannot read property 'length' of undefined完整代碼:import React from 'react';export default class EmailAnnotationForm extends React.Component {    constructor(props) {        super(props);        this.state = {            csrf: '',            subject: '',            emails: '',            comment: ''        }        this.handleInputChange = this.handleInputChange.bind(this);        this.handleFormSubmit = this.handleFormSubmit.bind(this);        this.handleClearForm = this.handleClearForm.bind(this);        this.input = React.createRef();    }    componentDidMount() {        console.log(this.state.subject.length) // renders correct value => 0        this.setState({subject: this.props.title });    }    handleInputChange(event) {        const target = event.target;        const value = target.type === 'checkbox' ? target.checked : target.value;        const name = target.name;        this.setState({            [name]: value        });    }    handleClearForm() {        this.setState({            csrf: '',            subject: '',            emails: '',            comment: ''        })    }    handleFormSubmit(event) {        var emailSubject;          {            this.state.subject ? emailSubject = this.state.subject : emailSubject = this.props.title        }; //        const body = {            subject: emailSubject,            emails: this.state.emails,            comment: this.state.comment        };
查看完整描述

1 回答

?
慕桂英546537

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

看來this.props.title是不確定的。


要解決此問題,請(qǐng)檢查this.props.title值,并僅在其具有有效值時(shí)更新狀態(tài)。像這樣:


componentDidMount() {

  if(this.props.title)

    this.setState({ subject: this.props.title });

}

建議:


不用subject在didMount方法中使用props值進(jìn)行更新,而是在構(gòu)造函數(shù)本身中執(zhí)行此操作,如下所示:


this.state = {

  csrf: '',

  subject: props.title || '',

  emails: '',

  comment: ''

}


查看完整回答
反對(duì) 回復(fù) 2021-04-29
  • 1 回答
  • 0 關(guān)注
  • 261 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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