【发布时间】:2020-09-07 12:05:51
【问题描述】:
我是 React 的初学者,在 componentDidMount 中,我从 Axios 检索一个响应对象,然后我将它设置为状态对象,即使它可以从外部函数访问,但它在render 方法,不知道怎么绑定状态才能访问render()
弹出的错误:未处理的拒绝(TypeError):无法读取未定义的属性“imageref”
export default class Product extends Component {
constructor(props) {
super(props);
const id = this.state.comicId;
console.log("This is printed from the constructor " + id);
}
state = {
comicId: "",
issueObjectState: "",
};
render(){
<img
src={this.state.issueObjectState.imageref}
alt="Image Description"
className="mx-auto img-fluid"/>
}
async componentDidMount() {
let state = [];
const id = this.props.location;
let comicId = id.data;
this.setState({ comicId: this.props.comicId });
let issueData = await axios.get(`http://localhost:5000/comic/${comicId}`);
let comicData = issueData.data;
if (comicData) {
this.setState({ issueObjectState: comicData });
console.log(this.state.issueObjectState);
}
}
}
非常感谢您的帮助
【问题讨论】:
-
将有助于显示有问题的代码。顺便说一句,您将
this.props.comicData分配给issueObjectState。这是为什么呢? -
你能检查你的api数据来了吗?
-
是的,我安慰了它,它有效
标签: javascript reactjs