【发布时间】:2018-05-09 07:48:36
【问题描述】:
我对 React 有疑问。 我有一个传递给 this.state 的数组。但我面临一个问题,我无法访问数组中的子元素。就我而言,我无法访问 this.state.folders[0]。
这是我的代码:
class DriveContent extends Component {
constructor () {
super();
let list_files = [];
let list_folders = [];
axios.get('/api/getFilesAndFolders').then((response) => {
for (var i = 0; i < response.data.length; i++) {
if (response.data[i]["isFile"] ==true){
list_files.push(response.data[i]);
}
else {
list_folders.push(response.data[i]);
}
}
}).catch((error) => {
console.error(error);
});
this.state = {files: list_files, folders: list_folders};
console.log(this.state.folders);
console.log(this.state.folders[0]);
}
这是返回 2 console.log 的内容:
// console.log(this.state.folders);
[]
0: "Commun"
1: "Privé"
2: "Public"
length: 3
//console.log(this.state.folders[0]);
undefined
提前致谢!
【问题讨论】: