KARTHIKEYAN.A
KARTHIKEYAN.A

Reputation: 20098

google-map-react: Element type is invalid: expected a string

If i use marker tag then my component not working and it throwing an error some thing like this

enter image description here

my code is:

import React, { Component } from 'react';
import GoogleMap, { Marker } from 'google-map-react';

class Maps extends Component {
  static defaultProps = {
    center: {lat: 22.741033, lng: 81.102441},
    zoom: 5
  };
  render() {
    return (
      <GoogleMap
        bootstrapURLKeys={{ key: ['AIzaSyAA1WZznnpeoP6hZz26UiARGNOhZhYLZek'] }}
        defaultCenter={this.props.center}
        defaultZoom={this.props.zoom}
        style={{height: '300px'}}
      >
      <Marker
        position={this.props.center}
      />
      </GoogleMap>
    );
  }
}

export default Maps;

Upvotes: 0

Views: 1308

Answers (2)

Noushad
Noushad

Reputation: 6781

It seems google-map-react doesnt export a component named Marker You can use your custom component as per their example here

Upvotes: 1

Noushad
Noushad

Reputation: 6781

It seems like the issue with exporting your component. I think GoogleMap is not exported by default in react-google-maps. Try importing it as a component.

import { GoogleMap, Marker } from "react-google-maps";

Also you might wanna take a look at withGoogleMap component from react-google-map.

Adding my example code below

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { withGoogleMap, GoogleMap, Marker } from "react-google-maps";

const MapWithAMarker = withGoogleMap(props => {
    return (
        <GoogleMap defaultZoom={12}  defaultCenter={{ lat: props.marker.lat, lng: props.marker.lng }}>
            {props.marker &&
                <Marker label={props.marker.name}  position={{ lat: props.marker.lat, lng: props.marker.lng }} />
            }
        </GoogleMap>
    );
});

export default class Map extends Component {
    render() {
        const { marker } = this.props;

        return (
            <MapWithAMarker marker={marker} containerElement={<div className="mapContainer full-flex" />} mapElement={<div className="map" />} />
        );
    }
}

Map.propTypes = {
    marker: PropTypes.object
};

Upvotes: 1

Related Questions