【发布时间】:2021-05-27 07:52:59
【问题描述】:
到目前为止,我已经尝试了几种在互联网上找到的方法,但都没有奏效。我现在尝试了 setTimeout 并像在代码中一样设置 isLoading。
至于加载数据,我确信它们显示正确,因为在段落中删除加载品牌后,数据在控制台中可见(更准确地说是在 React 'Components' 插件中)
这是 JSON 文件的样子:
[
{
id: 0,
brand: "Lorem0",
model: "Lorem",
dealer: "Lorem",
main_image: "Lorem",
sections: [
{
id: 0,
section_type: "",
section_title: "Lorem",
section_text: "Lorem Lorem Lorem Lorem Lorem",
image_left: "Lorem1.png",
image_center: null,
image_right: null
}
]
},
{
id: 1,
brand: "Lorem0",
model: "Lorem",
dealer: "Lorem",
main_image: "Lorem",
sections: [
{
id: 0,
section_type: "",
section_title: "Lorem",
section_text: "Lorem Lorem Lorem Lorem Lorem",
image_left: "Lorem1.png",
image_center: null,
image_right: null
}
]
},
]
这是来自 App.js 的代码:
state = {
isLoading: true,
}
carData = [];
componentDidMount() {
fetch("/data.json")
.then(response => response.json())
.then(data => {
this.setState({
carData: data,
isLoading: false
})
})
}
render() {
return (
<div className="App">
{!this.state.isLoading && <p>{this.carData[1].brand}</p>}
</div>
);
}
也许我犯了一个我看不到的错误,但在我看来一切都很好,但错误仍然出现。
TypeError:无法读取未定义的属性“品牌”
【问题讨论】:
标签: javascript reactjs components undefined settimeout