亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

未定義類型錯誤:無法讀取未定義的屬性“狀態”或“道具”

未定義類型錯誤:無法讀取未定義的屬性“狀態”或“道具”

縹緲止盈 2019-06-25 11:11:10
未定義類型錯誤:無法讀取未定義的屬性“狀態”或“道具”因此,我開始將我的應用程序從ES 2015轉換為ES6,它使用了Reaction。我有一個家長班和一個孩子班,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};但是,這會導致以下錯誤,狀態未定義它指的是,if (this.state.code == "") {     // Some functionality}知道是什么導致的嗎?
查看完整描述

2 回答

?
MYYA

TA貢獻1868條經驗 獲得超4個贊

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

家長:

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};

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

也可以將構造函數中的綁定指定為

家長:

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);}


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

TA貢獻1898條經驗 獲得超8個贊

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

不建議在呈現中直接綁定您的函數。建議您始終在構造函數中綁定它,因為如果直接在呈現中綁定,那么每當組件呈現Webpack時,就會在捆綁文件中創建一個新的函數/對象,這樣Webpack包的文件大小就會增加。由于許多原因,組件重新呈現例如:執行setState,但是如果將它放置在構造函數中,則只調用一次。

不建議執行以下內容

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

始終在構造函數中執行此操作,并在需要的地方使用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,則不需要手動綁定,但如果需要,則可以。如果希望避開與范圍相關的問題和手動函數/對象綁定,可以使用箭頭函數。

對不起,如果我的手機上有任何輸入錯誤


查看完整回答
反對 回復 2019-06-25
  • 2 回答
  • 0 關注
  • 1855 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號