Не удается получить экземпляр карты Google - PullRequest
0 голосов
/ 09 апреля 2019

Я хочу получить экземпляр карты в методе componentDidMount(), но не удалось.

Пожалуйста, смотрите мой следующий код, я хочу получить экземпляр карты по

const map = this.context[MAP];

Но регистр if всегда ложен. Кстати, el в ссылке всегда не определено.

import React, { Component } from 'react';
import {compose, withProps, onlyUpdateForPropTypes} from "recompose";
import {withScriptjs, withGoogleMap, GoogleMap, Marker} from 'react-google-maps';
import {MAP} from 'react-google-maps/lib/constants';
import PropTypes from "prop-types";

const MyMapComponent = compose(
  withProps({
    googleMapURL: "https://maps.googleapis.com/maps/api/js?key=v2&libraries=places,geometry,drawing",
    loadingElement: <div style={{height: '100%'}} />,
    containerElement: <div style={{height: '800px'}} />,
    mapElement: <div style={{height: '100%'}} />,
  }),
  withScriptjs,
  withGoogleMap
)(props =>
  <GoogleMap
    defaultZoom={15}
    defaultCenter={{lat: 47.6129432, lng: -122.9716967}} >

    </GoogleMap>
);

export default class MapContainer extends Component {
  static contextTypes = { [MAP]: PropTypes.object };

  render() {
    return (
      <MyMapComponent 
        ref={(el) => {this.mapNode = el}} />
    )
  }

  componentDidMount() {
    console.log("Entry into componentDidMount : " + this.mapNode);

    if (this.context[MAP]) {
      const map = this.context[MAP];
      console.log("The map instance is: " + map);
    }
  }
}

Кто-нибудь знает, как получить экземпляр карты в методе componentDidMount()?

...