그래서 지금 당장 가지고있는 문제는 첫 번째 클릭에서 내 GUI가 다시 렌더링됩니다. 그러나 두 번째 클릭시 다시 렌더링되지 않습니다. 내 렌더링이 "graphicLayers.map"을 통해 바인딩하는 "graphicLayers"의 상태를 업데이트하지 않기 때문이라고 생각합니다. 즉, 어쨌든 내 이론 (그것의 작동에도 불구하고 첫 번째 클릭?하지만 이후 두번째 클릭 또는 아무것도) 나는 graphicLayers의 setState를 업데이트를 강제로 시도데이터가 변경되지 않은 경우에도 setState를 사용하여 강제로 다시 렌더링 할 수 있습니까? 내 GUI가 업데이트되지 않습니다.
을하지만,이
처럼 .. 작동하지 않는 것let graphicLayersCopy = Object.assign([], this.state.graphicLayers);
this.setState({graphicLayers: graphicLayersCopy});
하지만 작동하지 않습니다. 나는 디버거를 통해 데이터를 올바르게 설정한다는 것을 알고, 새로 고침 (상태 저장 및 상태 재로드)하면 GUI가 올바르게 렌더링됩니다.
어쨌든 변수를 다시 렌더링하도록 강제 할 수 있습니까? 값을 변경하지 않아도 어떻게됩니까?
생성자
constructor(props, context) {
super(props, context);
this.state = {
graphicLayers: [id1, id2, id3],
graphicLayersById: {
id1: { ... },
id2: { ... },
id3: { ... }
}
this.addLayerClick = this.addLayerClick.bind(this);
};
render() {
return (
<div>
{this.state.graphicLayers.map((id) =>
<GraphicLayer addLayerClick={this.addLayerClick.bind(this)} />
)}
</div>
);
}
addLayerClick
addLayerClick() {
... change some property in graphicLayersById dictionary ...
self.setState({ graphicLayersById: newDataHere });
}
편집 렌더링 : 얘들 아 그래서 난 내 말에 문제를 발견하고, 정확하게 여기에 표시 아닙니다.
내 addLayerClick() 실제로 호출을 수신하는 다른 함수를 호출하고 내부 상태를 설정합니다. setState가 콜백 함수에서 호출되기 때문에 이상한데, addLayerClick() 자체에 setState를 두어 작동하도록했습니다. 아직도 doens't가 작동하지 않는 이유는 알고 있지만 적어도 여러분 모두를 upvote 할 것입니다.
listenFunction() {
let self = this;
this.firebaseWrapper.storage.on('graphicLayersById', function (save) {
if (save) {
self.setState({ graphicLayersById: save }); // FOR SOME REASON THIS DOESN'T UPDATE THE GUI THE 2nd CLICK. The data is correct though and I see it going here on a breakpoint, but GUI won't update unless i setState in the actual button
}
else {
self.setState({ graphicLayersById: undefined });
}
});
} 사실
.bind (this)가 두 번 쓰여집니다. –
@TreefishZhang 의견을 보내 주셔서 감사합니다. 나는 실제로 그것이 문제가되는 것을 발견했을 때 거기에 추가했다. 나는 당신의 코멘트에 기반하여 생성자에서 하나를 남겨 두었지만 그것을 제거했지만 여전히 불행히도 같은 문제를 가지고있다. – user1189352
나는 window.location.reload()를 사용했다. 재 렌더링을 강요하지만 비싸다. 전체 페이지가 다시 그려진다. –