배열이 처음 렌더링 될 때 올바른 순서로 표시되지만, 변경된 경우 렌더링 된 순서는 동일하게 유지됩니다. 예를 들어React : 배열이 처음부터 올바른 순서로 렌더링되지 않습니다.
:
On the console (the current order, correct):
3
1
2
4
Rendered as DOM (the original order, incorrect):
1
2
3
4
이 올바른 순서로 렌더링하는 데 실패하는 이유는 어떤 생각 :에
construct() {
this.state = {
test_array: [1,2,3,4]
}
let self = this;
setTimeout(function(){
self.scramble();
}, 5000);
}
scramble() {
this.state.test_array = [3,1,2,4];
this.setState(self.state);
}
render() {
this.state.test_array.forEach(function(item){
console.log(item);
});
return (
<div>
{this.state.test_array}
</div>
);
}
결과?
좋은 교훈 : 콘솔에서 키 경고를 무시하지 마십시오. "경고 : 배열 또는 반복기의 각 하위에는 고유 한"키 "소품이 있어야합니다." 솔루션은 https://reactjs.org/docs/lists-and-keys.html#keys를 참조하십시오. –