gpt4 book ai didi

reactjs - 出现错误 : Cannot read property state of undefined

转载 作者:行者123 更新时间:2023-12-03 13:24:18 25 4
gpt4 key购买 nike

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 代码实现方面存在一些技术上的错误。

首先,使用 ES6 编写类的风格,任何需要访问 Class 属性的函数都需要显式绑定(bind)。在您的情况下,您需要使用构造函数中的箭头函数绑定(bind)来绑定(bind)handleSubmit函数。

有关更多详细信息,请参阅此答案:Why and when do we need to bind functions and eventHandlers in React?

其次:您在 componentWillMount 函数中设置了异步请求,并在其成功响应中设置了状态。但是,在组件渲染后会触发 componentWillMount 中的 setState ,因此您仍然需要进行未定义的检查。您应该使用 componentDidMount 生命周期函数来处理异步请求。

检查此答案是否有 AJAX request in componentDidMount or componentWillMount

第三: setState 是异步的,因此在 setState 函数之后记录状态值不会导致显示正确的输出。请改用 setState 回调

有关更多详细信息,请参阅这些答案:

calling setState doesn't mutate state immediately

When to use React setState callback

代码:

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

关于reactjs - 出现错误 : Cannot read property state of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47027035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com