3
React에 문제가 있습니다. 나는 this.state에게 전달하는 배열을 가지고있다. 하지만 문제가 발생했습니다. 배열의 하위 요소에 액세스 할 수 없습니다. 내 경우에는 this.state.folders [0]에 액세스 할 수 없습니다. 사전에반응이 상태. 정의되지 않은 배열
// console.log(this.state.folders);
[]
0: "Commun"
1: "Privé"
2: "Public"
length: 3
//console.log(this.state.folders[0]);
undefined
감사 :
다음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 ('render', this.props, this.state);'를 개발할 때 렌더 함수에 항상 이것을 넣으면 편리 할 수있다. 그래서 모든 업데이트를 볼 수 있고 최적화를해야 줄이는 지 볼 수있다. 'shouldComponentUpdate' 함수로 다시 렌더링합니다. – Scott