簡體   English   中英

反應,this.props未定義

[英]React, this.props is not defined

我知道這是一個非常普遍的問題,在發布此信息之前,我調查了許多其他投訴。

我有班級家長

class Parent extends React.Component {
  constructor() {
    super();
    this.state = { 
      ...
    };

  }
  ....

  render() {
    return (
      <MuiThemeProvider theme={materialTheme}>
        <Child 
          ref={...} 
          groupId={this.state.groupId}
          groupUniqueId={this.state.groupUniqueId} />
      </MuiThemeProvider>
    );
  }
}

還有一個班級的孩子

class Child extends React.Component {
  constructor() {
    super(props);
    this.state = { 
      ...
    };
    ...
  }
  getUsers() { 
    const url = `/someurl/${this.props.groupId}`;
    ...
  }

  render() {
    return (...);
  }
}

export default Child;

但是,在Child班上,我遇到了一個錯誤

“未捕獲的ReferenceError:道具未定義”

有什么明顯的我想念的東西嗎? 謝謝!

發生這種情況是因為您的this沒有引用類。 它指的是您的功能。 您可以使用箭頭函數,也可以將其綁定到構造函數中的函數。 只需添加以下行

constructor() {
super(props);
  this.state = { 
    ...
  }
   this.getUsers = this.getUsers.bind(this)
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM