出现错误:无法读取未定义的属性状态 [英] getting error: Cannot read property state of undefined
问题描述
import React, { Component } from "react";
import FormUpdate from "../components/formUpdate";
import { fetchClothingItem, updateClothingItem } from "../actions/crud";
export default class Update extends Component {
constructor(props) {
super(props);
this.state = {
updateClothingItem: {}
};
}
componentWillMount() {
fetchClothingItem(this.props.match.params.postId)
.then(data => {
this.setState(state => {
state.updateClothingItem = data;
return state;
});
console.log("data", data);
//HERE IT IS RETURNING EXPECTED DATA
console.log("this.state.updateClothingItem",this.state.updateClothingItem)
})
.catch(err => {
console.error("err", err);
});
}
handleSubmit(data) {
//HERE IT IS THROWING:
> "TypeError: Cannot read property 'state' of undefined"
console.log("this.state.updateClothingItem", this.state.updateClothingItem);
updateClothingItem(this.state.updateClothingItem.id, data); this.props.router.push("/update");
}
render() {
return (
<div>
<FormUpdate
//onSubmit={this.handleSubmit.bind(this)}
id={this.state.updateClothingItem.id}
name={this.state.updateClothingItem.name}
sleeveLength={this.state.updateClothingItem.sleeveLength}
fabricWeight={this.state.updateClothingItem.fabricWeight}
mood={this.state.updateClothingItem.body}
color={this.state.updateClothingItem.color}
/>
<button
type="submit"
onClick={this.handleSubmit}
className="addItemButton"
>
Button
</button>
</div>
);
}
}
推荐答案
在React代码实现方面,有些技术上是错误的.
There are a few things that are technically wrong in terms of React code implementation.
首先,使用ES6风格编写类,需要访问Class属性的任何函数都必须明确地为binded
.在您的情况下,您需要使用arrow function
或binding in constructor
绑定handleSubmit函数.
Firstly, With ES6 style of writing a class, any function that needs to access the Class properties need to be explicitly binded
. In your case you need to bind the handleSubmit function using arrow function
of or binding in constructor
.
See this answer for more details: Why and when do we need to bind functions and eventHandlers in React?
第二步::您已经在componentWillMount函数中设置了异步请求,并在其成功响应中设置了状态.但是,在渲染组件后会触发在componentWillMount
中使用setState
,因此您仍然需要进行未定义的检查.相反,您应该对异步请求使用componentDidMount
生命周期功能.
Secondly: You have your async request set up in the componentWillMount function and in the success response of it, you are setting state. However using setState
in componentWillMount
is triggered after the component is rendered so you still need to have an undefined check. You should instead make use of componentDidMount
lifecycle function for async requests.
Check this answer on whether to have AJAX request in componentDidMount
or componentWillMount
第三: setState是异步的,因此在setState函数之后记录状态值将不会导致显示正确的输出.请使用setState callback
.
Third: setState is asynchronous and hence logging the state values after the setState function won't result in the correct output being displayed. Use the setState callback
instead.
有关更多详细信息,请参见以下答案:
See these answers for more details:
代码:
export default class Update extends Component {
constructor(props) {
super(props);
this.state = {
updateClothingItem: {}
};
}
componentDidMount() {
fetchClothingItem(this.props.match.params.postId)
.then(data => {
this.setState(state => {
state.updateClothingItem = data;
return state;
});
console.log("data", data);
//HERE IT IS RETURNING EXPECTED DATA
console.log("this.state.updateClothingItem",this.state.updateClothingItem)
}) // this statement will not show you correct result since setState is async
.catch(err => {
console.error("err", err);
});
}
handleSubmit = (data) => { . // binding using arrow function here
console.log("this.state.updateClothingItem", this.state.updateClothingItem);
updateClothingItem(this.state.updateClothingItem.id, data); this.props.router.push("/update");
}
render() {
return (
<div>
<FormUpdate
//onSubmit={this.handleSubmit.bind(this)}
id={this.state.updateClothingItem.id}
name={this.state.updateClothingItem.name}
sleeveLength={this.state.updateClothingItem.sleeveLength}
fabricWeight={this.state.updateClothingItem.fabricWeight}
mood={this.state.updateClothingItem.body}
color={this.state.updateClothingItem.color}
/>
<button
type="submit"
onClick={this.handleSubmit}
className="addItemButton"
>
Button
</button>
</div>
);
}
}
这篇关于出现错误:无法读取未定义的属性状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!