【发布时间】:2019-04-30 02:13:27
【问题描述】:
我正在尝试渲染一个使用 fetch() 调用从 Django REST 框架获取数据的组件。由于我的数据的结构方式,我必须使用 fetch 两次。
当我在收到数据后尝试 setState 并将其传递给组件和 console.log 时,我得到空括号 {},当我展开它时,我有数据但我无法访问它。但是如果我展开它,我可以看到数据
示例: {} 酒店:123, 办公室:456
data.Hotel = 未定义
通常当我控制台记录数据时,它看起来像 {酒店:123,办公室:456}
我构建反应的方式是错误的还是我遗漏了什么? 我是新来的反应,所以任何帮助将不胜感激。
DataProvider.js
import React, { Component } from "react";
import PropTypes from "prop-types";
class DataProvider extends Component {
static propTypes = {
endpoint: PropTypes.string.isRequired,
};
state = {
project: {},
run_set: [],
project_eui: 0,
run_euis: {},
loaded: false,
placeholder: "Loading.."
};
componentDidMount() {
let currentComponent = this;
var project = {}
var run_set = {}
var project_eui = 0
var run_euis = {}
var run_sets = []
try {
const data = {username: 'username', password: 'password'};
fetch('URL', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
}).then(function(response) {
return response.json();
}).then(function(data){
return data.token
}).then(function(token){
try {
fetch(currentComponent.props.endpoint, {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'authorization': 'Token ' + token
}
}).then(function(response) {
return response.json();
}).then(function(project){
project = project
project_eui = project.eui
var numBuildingType = project.run_set.length
for (let i = 0; i < numBuildingType; i++) {
try {
//URL from 1st fetch call
fetch(project.run_set[i], {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'authorization': 'Token ' + token
}
}).then(function(response) {
return response.json();
}).then(function(run_set){
run_sets.push(run_set)
run_euis[run_set.building_type] = run_set.eui
})
} catch (e) {
console.log(e);
}
}
})
} catch (e) {
console.log(e);
}
})
} catch (e) {
console.log(e);
}
currentComponent.setState({ project: project, project_eui: project_eui, run_set: run_sets, run_euis: run_euis, loaded: true });
}
render() {
const { project, project_eui, run_set, run_euis, loaded, placeholder } = this.state
if (loaded == true) {
const myJson = { "project": this.state.project, "run_set": this.state.run_set, "project_eui": this.state.project_eui, "run_euis": this.state.run_euis}
return this.props.render(myJson)
} else {
return <p> {placeholder} </p>
}
}
}
结构数据.js
...
const StructureData = ({ data }) => {
console.log(data.run_euis) --------------------> {}
return (
<div>
<p>something</p>
</div>
);
}
export default StructureData;
App.js
...
return <DataProvider endpoint= {projectApiEndpoint}
render= {data => <StructureData data={data} />}
/>
...
【问题讨论】: