[英]react.js Cannot read property 'setState' of undefined
Here is my Profile
class : 这是我的Profile
课:
class Profile extends React.Component {
state={email:'',userName:'',userID:''};
getData()
{
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
{
console.log(this.responseText);
this.setState({'userID':JSON.parse(this.responseText).userID});
console.log(this.responseText);
Profile.this.setState({userID:JSON.parse(this.responseText).userID});
}
};
request.open('GET', 'http://localhost:8080/user/1');
request.send();
}
componentWillMount() {
this.getData()
}
render() {
return(
<div>
{this.props.userID}
</div>
);
}
}
export default Profile;
It gives me error saying Cannot read property 'setState' of undefined . 它给我一个错误,提示无法读取undefined的属性“ setState” 。
Error in line : 发生错误:
Profile.this.setState({userID:JSON.parse(this.responseText).userID});
What's wrong here? 怎么了 How this can be resolved ? 如何解决?
You have a number of mistakes here is the correct way of doing this 您有很多错误,这是正确的做法
class Profile extends React.Component {
constructor(props) {
super(props);
// You need to set the initial state inside the constructor
this.state = { email: "", userName: "", userID: "" };
}
getData() {
// use let instead of var
let request = new XMLHttpRequest();
// call request.open outside of the handler
request.open("GET", "http://localhost:8080/user/1");
// use an arrow function so that you will have access to the class this context
request.onreadystatechange = () => {
// you must directly reference request, not this
if (request.readyState === 4 && request.status === 200) {
const { userID } = JSON.parse(request.responseText);
// destruct instead of typing the entire thing
// use the shorthand way of creating object with same name properties
this.setState({ userID });
}
};
// call request.send outside of the handler
request.send();
}
// use componentDidMount , componentWillMount is deprecated
componentDidMount() {
this.getData();
}
render() {
// you should get the userID from the this.state not this.props
return <div>{this.state.userID}</div>;
}
}
There is no Profile.this
, Profile
is a class, you'll have to keep a reference to the instance or use an arrow function instead of a normal function, the easiest way here will be to keep a reference to the component, here is an example: 没有Profile.this
, Profile
是一个类,您必须保留对实例的引用或使用箭头函数而不是常规函数,这里最简单的方法是保留对组件的引用,这是一个例子:
getData()
{
var that = this;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
{
console.log(this.responseText);
that.setState({'userID':JSON.parse(this.responseText).userID});
}
};
request.open('GET', 'http://localhost:8080/user/1');
request.send();
}
try this 尝试这个
you have multiple this contexts that refer to different things. 您有多个引用不同事物的上下文。
getData = () => {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState === 4 && request.status === 200) {
{
console.log(this.responseText);
this.setState({'userID':JSON.parse(request.responseText).userID});
console.log(this.responseText);
this.setState({userID:JSON.parse(request.responseText).userID});
}
};
request.open('GET', 'http://localhost:8080/user/1');
request.send();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.