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

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

未定義類型錯(cuò)誤:無法讀取未定義的屬性“狀態(tài)”或“道具”

未定義類型錯(cuò)誤:無法讀取未定義的屬性“狀態(tài)”或“道具”

縹緲止盈 2019-06-25 11:11:10
未定義類型錯(cuò)誤:無法讀取未定義的屬性“狀態(tài)”或“道具”因此,我開始將我的應(yīng)用程序從ES 2015轉(zhuǎn)換為ES6,它使用了Reaction。我有一個(gè)家長班和一個(gè)孩子班,export default class Parent extends Component {     constructor(props) {         super(props);         this.state = {             code: ''         };     }     setCodeChange(newCode) {         this.setState({code: newCode});     }     login() {         if (this.state.code == "") {             // Some functionality         }     }     render() {         return (             <div>                 <Child onCodeChange={this.setCodeChange} onLogin={this.login} />             </div>         );     }}兒童班,export default class Child extends Component {     constructor(props) {         super(props);     }     handleCodeChange(e) {         this.props.onCodeChange(e.target.value);     }     login() {         this.props.onLogin();     }     render() {         return (             <div>                 <input name="code" onChange={this.handleCodeChange.bind(this)}/>             </div>             <button id="login" onClick={this.login.bind(this)}>         );     }}Child.propTypes = {     onCodeChange: React.PropTypes.func,     onLogin: React.PropTypes.func};但是,這會(huì)導(dǎo)致以下錯(cuò)誤,狀態(tài)未定義它指的是,if (this.state.code == "") {     // Some functionality}知道是什么導(dǎo)致的嗎?
查看完整描述

2 回答

?
MYYA

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

可以使用箭頭函數(shù)綁定函數(shù)。您需要在子組件和父組件中綁定您的函數(shù)。

家長:

export default class Parent extends Component {
    constructor(props) {
        super(props);
        this.state = {
            code: ''
        };
    }

    setCodeChange = (newCode) => {
        this.setState({code: newCode});
    }


    login = () => {
        if (this.state.code == "") {
            // Some functionality
        }
    }

    render() {
        return (
            <div>
                <Child onCodeChange={this.setCodeChange} onLogin={this.login} />
            </div>
        );
    }}

兒童

export default class Child extends Component {
    constructor(props) {
        super(props);
    }

    handleCodeChange = (e) => {
        this.props.onCodeChange(e.target.value);
    }

    login = () => {
        this.props.onLogin();
    }

    render() {
        return (
            <div>
                <input name="code" onChange={this.handleCodeChange}/>
            </div>
            <button id="login" onClick={this.login}>
        );
    }}Child.propTypes = {
    onCodeChange: React.PropTypes.func,
    onLogin: React.PropTypes.func};

還有其他綁定函數(shù)的方法,例如您正在使用的方法,但是您也需要對父組件這樣做。<Child onCodeChange={this.setCodeChange.bind(this)} onLogin={this.login.bind(this)} />

也可以將構(gòu)造函數(shù)中的綁定指定為

家長:

constructor(props) {
    super(props);
    this.state = {
        code: ''
    };
 this.setCodeChange = this.setCodeChange.bind(this);
 this.login = this.login.bind(this);}

兒童

constructor(props) {
    super(props);
    this.handleCodeChange = this.handleCodeChange.bind(this);
    this.login = this.login.bind(this);}


查看完整回答
反對 回復(fù) 2019-06-25
?
汪汪一只貓

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

我同意@Shubham Kathri給出的所有不同的解決方案,但渲染中的直接約束力除外。

不建議在呈現(xiàn)中直接綁定您的函數(shù)。建議您始終在構(gòu)造函數(shù)中綁定它,因?yàn)槿绻苯釉诔尸F(xiàn)中綁定,那么每當(dāng)組件呈現(xiàn)Webpack時(shí),就會(huì)在捆綁文件中創(chuàng)建一個(gè)新的函數(shù)/對象,這樣Webpack包的文件大小就會(huì)增加。由于許多原因,組件重新呈現(xiàn)例如:執(zhí)行setState,但是如果將它放置在構(gòu)造函數(shù)中,則只調(diào)用一次。

不建議執(zhí)行以下內(nèi)容

<Child onCodeChange={this.setCodeChange.bind(this)} onLogin={this.login.bind(this)} />

始終在構(gòu)造函數(shù)中執(zhí)行此操作,并在需要的地方使用ref。

constructor(props){
  super(props);
  this.login = this.login.bind(this);
  this.setCodeChange = this.setCodeChange.bind(this);}<Child onCodeChange={this.setCodeChange} onLogin={this.login} />

如果您正在使用ES6,則不需要手動(dòng)綁定,但如果需要,則可以。如果希望避開與范圍相關(guān)的問題和手動(dòng)函數(shù)/對象綁定,可以使用箭頭函數(shù)。

對不起,如果我的手機(jī)上有任何輸入錯(cuò)誤


查看完整回答
反對 回復(fù) 2019-06-25
  • 2 回答
  • 0 關(guān)注
  • 1876 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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