2017-02-15 5 views
0

Я пытаюсь получить геолокацию, setstate, а затем передать ее дочернему компоненту. Проблема заключается в том, что компонент, который нуждается в состоянии в качестве реквизита, отображается перед установкой состояния геолокации. Я подумал, что когда государство изменится, React переигнет компонент, который использует это состояние.Передача геолокации от родителя к ребенку в реакции

class App extends Component { 
 
\t constructor(props) { 
 
\t \t super(props) 
 
\t \t this.state = { 
 
\t \t \t location: null, 
 
\t \t \t venues: [], 
 
\t \t \t markers: [] 
 
\t \t } 
 
\t \t this.getCurrentLocation = this.getCurrentLocation.bind(this) 
 
\t \t this.setCurrentLocation = this.setCurrentLocation.bind(this) \t \t 
 
\t } 
 
    
 
     setCurrentLocation(currentLocation) { 
 
\t \t this.setState({location: currentLocation})  \t \t 
 
\t } 
 

 
\t getCurrentLocation() { 
 
\t \t let self = this; \t 
 
\t \t navigator.geolocation.getCurrentPosition(function(position) { \t \t \t \t \t 
 
\t \t \t let updatedLocation = { 
 
\t \t \t \t lat: position.coords.latitude, 
 
\t \t \t \t lng: position.coords.longitude 
 
\t \t \t } 
 
\t \t \t self.setCurrentLocation(updatedLocation) \t \t \t 
 
\t \t }) 
 
\t } 
 
    
 
     render() { 
 
\t \t const markers = [ 
 
\t \t \t { 
 
\t \t \t \t location: { 
 
\t \t \t \t \t lat: this.state.location.lat, 
 
\t \t \t \t \t lng: this.state.location.lng 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t ] 
 
\t \t return (
 
\t \t \t <div className="main-container"> 
 
\t \t \t \t <h1>Render a Map</h1> 
 
\t \t \t \t <div className="inner-container"> 
 
\t \t \t \t \t <div className="map"> 
 
\t \t \t \t \t \t <Map center={this.state.location} markers={markers}/> 
 
\t \t \t \t \t </div> 
 
\t \t \t \t \t 
 
\t \t \t \t </div> 
 
\t \t \t </div> 
 
\t \t) 
 
\t } 
 
}

Это компонент, который использует местоположение

class Map extends Component { 
 
\t constructor(props) { 
 
\t \t super(props) \t \t 
 
\t } 
 

 
\t render() { \t \t 
 
\t \t const mapContainer = <div className="map-container"></div> 
 
\t \t const markers = this.props.markers.map((marker, i) => { 
 
\t \t \t const place = { 
 
\t \t \t \t position: { 
 
\t \t \t \t \t lat: marker.location.lat, 
 
\t \t \t \t \t lng: marker.location.lng 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t \t return <Marker key={i} {...place} /> 
 
\t \t }) 
 
\t \t return (
 
\t \t \t <GoogleMapLoader 
 
\t \t \t \t containerElement={mapContainer} 
 
\t \t \t \t googleMapElement={ 
 
\t \t \t \t \t <GoogleMap 
 
\t \t \t \t \t \t defaultZoom={15} 
 
\t \t \t \t \t \t defaultCenter={this.props.center} 
 
\t \t \t \t \t \t options={{streetViewController: false, mapTypeControl: false}}> 
 
\t \t \t \t \t \t { markers } 
 
\t \t \t \t \t </GoogleMap> 
 
\t \t \t \t } /> 
 
\t \t) 
 
\t } 
 
}

Спасибо за вашу помощь.

ответ

0

Не откладывайте ребенка до тех пор, пока у вас не будет данных.

{this.state.location && <Map .../>}

Это, как говорится, компонент Карты должен вновь сделать каждый раз изменить его реквизит, так что я бы проверить в React DEV-инструментах, которые location состояния действительно меняются, когда вы ожидаете.

+0

Прибито гвоздем, спасибо! –

Смежные вопросы