우선 ReactJS에서 새로 생겼습니다. MapComponent에 마커를 표시하고 싶습니다. 나는이 일을했지만 솔직히 말해서 더 좋은 방법이 있어야한다고 생각합니다 ... JSON에서로드 된 상위 구성 요소에서 소품을받습니다. 각 항목의 좌표를 내 JSON에서 내 MapComponent의 상태로 마커로 전달하고 싶습니다. Google지도에 반응 Google지도 솔루션을 사용했습니다. 누군가이 케이스에서 가장 좋은 방법이 무엇인지 조언 해 줄 수 있습니까? 모든 팁을 가져 주셔서 감사합니다. 0.14는 (제 생각 엔) 이후 소품을 통해 상태를 통과하기 때문에ReactJS의 구성 요소 상태
export class MapComponent extends React.Component {
constructor(props) {
super(props)
this.state = {
markers: []
}
}
getMarkers() {
if (this.props.data.rows) {
var markers = this.props.data.rows.map(function(item, i) {
return {
position: {
lat: item.coordinate[0],
lng: item.coordinate[1]
},
item: item
}
});
this.setState({
markers: markers
});
}
}
shouldComponentUpdate(nextProps, nextState) {
return true;
}
componentDidUpdate() {
this.getMarkers();
}
render() {
return (
<div className={styles.map}>
<GoogleMapLoader
containerElement={
<div
{...this.props}
style={{
height: "100%",
}}
/>
}
googleMapElement={
<GoogleMap
ref={(map) => console.log(map)}
defaultZoom={9}
defaultCenter={{lat: 52.379189, lng: 4.899431}}>
{this.state.markers.map((marker, index) => {
return (
<Marker
{...marker}
/>
);
})}
</GoogleMap>
}
/>
</div>
);
}
}
당신 소품으로 더 쉽게 할 수 있습니다 – webdeb
@webdeb 무엇을 제안 하시겠습니까? 내 항목을 반복 한 다음 Google지도의 좌표를 반복해야합니까? – burakukula
제 제안은 이전 루프를 준비하는 대신 단일 루프에서 즉시 수행하는 것입니다. 그런 다음 다른 루프를 실행하여 렌더링합니다. 내 대답은 – webdeb