[英]React, this.props is not defined
I know this is a very common problem and I looked into many other complaints before posting this. 我知道这是一个非常普遍的问题,在发布此信息之前,我调查了许多其他投诉。
I have class Parent 我有班级家长
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>
);
}
}
And a class Child 还有一个班级的孩子
class Child extends React.Component {
constructor() {
super(props);
this.state = {
...
};
...
}
getUsers() {
const url = `/someurl/${this.props.groupId}`;
...
}
render() {
return (...);
}
}
export default Child;
However, in the class Child, I get an error 但是,在Child班上,我遇到了一个错误
"Uncaught ReferenceError: props is not defined" “未捕获的ReferenceError:道具未定义”
Is there something obvious that I am missing? 有什么明显的我想念的东西吗? Thanks! 谢谢!
This is happening because your this
is not referencing a class. 发生这种情况是因为您的this
没有引用类。 It is referring to your function. 它指的是您的功能。 You can either use arrow functions or bind this to your function in constructor. 您可以使用箭头函数,也可以将其绑定到构造函数中的函数。 Just add below line 只需添加以下行
constructor() {
super(props);
this.state = {
...
}
this.getUsers = this.getUsers.bind(this)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.