2016-07-12 5 views
1

우선 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> 
    ); 
    } 
} 
+0

당신 소품으로 더 쉽게 할 수 있습니다 – webdeb

+0

@webdeb 무엇을 제안 하시겠습니까? 내 항목을 반복 한 다음 Google지도의 좌표를 반복해야합니까? – burakukula

+0

제 제안은 이전 루프를 준비하는 대신 단일 루프에서 즉시 수행하는 것입니다. 그런 다음 다른 루프를 실행하여 렌더링합니다. 내 대답은 – webdeb

답변

0

, 당신이 구성 요소 클래스를 만드는 난형 수 있습니다, 당신은 실제로 단순한 재현 구성 요소 필요가 지원됩니다. 이 표현 구성 요소은 단지 기능이며, render 함수를 나타냅니다. 이 기능은 그래서 당신은 구성 요소보다 깨끗한 모양

PARAM

props을 :

const MapComponent = (props) => { 
    return (
    <div className={styles.map}> 
     <GoogleMapLoader 
     containerElement={ 
      <div 
      {...props} 
      style={{height: "100%"}} 
      /> 
     } 
     googleMapElement={ 
      <GoogleMap 
      ref={(map) => console.log(map)} 
      defaultZoom={9} 
      defaultCenter={{lat: 52.379189, lng: 4.899431}}> 
       {props.data.rows.map((item, index) => { 
       const position = { 
        lat: item.coordinate[0], 
        lng: item.coordinate[1] 
       }; 

       return (
        <Marker 
        key={index} 
        position={position} 
        item={item} 
        /> 
      ); 
      })} 
      </GoogleMap> 
     } 
     /> 
    </div>); 
} 
+0

고맙습니다. 이것은 내 아이디어보다 훨씬 나은 해결책입니다. 다시 고마워. – burakukula

+0

기꺼이 도와 드리겠습니다. – webdeb

0

첫째, @webdeb 말했듯이, 더 나은 소품을 사용하여 올바른 데이터를 전송 할 수 있습니다. 상태를 사용하면 불필요한 구성 요소 다시 그리기가 발생합니다. 어떤 이유로 당신이 소품과 당신이 설명으로 상태를 처리 할 필요가있는 경우 이 두 번째로, componentDidUpdate 방법은이 목적에 적합하지 않습니다, 당신은 그것을 대체해야 componentWillReceiveProps

뭔가 같은 :

import React, {Component} from 'react'; 
export class MapComponent extends Component { 

    constructor(props) { 
     super(props) 
     this.state = { 
      markers: [] 
    } 
    } 
    componentDidMount() { 
     this.getMarkers(this.props); 
    } 
    componentWillReceiveProps(nextProps){ 
     if(this.props.data!==nextProps.data){ 
      const markers = this.getMarkers(nextProps); 
      this.setState({ 
       markers: markers 
      }); 
     } 
    } 

    getMarkers(props) { 
     let markers = []; 
     if (props.data.rows) { 
      markers = props.data.rows.map(function(item, i) { 
       return { 
       position: { 
        lat: item.coordinate[0], 
        lng: item.coordinate[1] 
       }, 
       item: item 
       } 
      }); 

     } 
     return markers; 
    } 
} 
+0

안녕하세요. 고맙습니다. 나는 webdeb 해결책으로 갈 것이다. 그러나 이런 식으로이 사건을 다루는 법을 알면 아주 좋습니다. 도와 줘서 고마워. – burakukula

관련 문제