javascript - 未捕获的 TypeError : inst. 渲染不是函数; react 谷歌地图

标签 javascript google-maps reactjs

我在尝试遵循 https://tomchentw.github.io/react-google-maps/ 的 React 应用程序中收到 Uncaught TypeError: inst.render is not a function

src/components/MapShowEmployers.js:

/* global google */
import _ from "lodash";

import {
  default as React,
  Component,
} from "react";

import Helmet from "react-helmet";

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

/*
 * This is the modify version of:
 * https://developers.google.com/maps/documentation/javascript/examples/event-arguments
 *
 * Add <script src="https://maps.googleapis.com/maps/api/js"></script> to your HTML to provide google.maps reference
 */
const GettingStartedGoogleMap = withGoogleMap(props => (
      <GoogleMap
        ref={props.onMapLoad}
        defaultZoom={3}
        defaultCenter={{ lat: 30.3, lng: -97.743 }}
        onClick={props.onMapClick}
      >
        {props.markers.map(marker => (
          <Marker
            {...marker}
            onRightClick={() => props.onMarkerRightClick(marker)}
          />
        ))}
      </GoogleMap>
    ));


export default class MapShowEmployers extends Component {

  state = {
    markers: [{
      position: {
        lat: 30.4,
        lng: -97.845,
      },
      key: `Austin`,
      defaultAnimation: 2,
    }],
  };

  handleMapLoad = this.handleMapLoad.bind(this);
  handleMapClick = this.handleMapClick.bind(this);
  handleMarkerRightClick = this.handleMarkerRightClick.bind(this);

  handleMapLoad(map) {
    this._mapComponent = map;
    if (map) {
      console.log(map.getZoom());
    }
  }

  /*
   * This is called when you click on the map.
   * Go and try click now.
   */
  handleMapClick(event) {
    const nextMarkers = [
      ...this.state.markers,
      {
        position: event.latLng,
        defaultAnimation: 2,
        key: Date.now(), 
      },
    ];
    this.setState({
      markers: nextMarkers,
    });

    if (nextMarkers.length === 3) {
      this.props.toast(
        `Right click on the marker to remove it`,
        `Also check the code!`
      );
    }
  }

  handleMarkerRightClick(targetMarker) {
    /*
     * All you modify is data, and the view is driven by data.
     * This is so called data-driven-development. (And yes, it's now in
     * web front end and even with google maps API.)
     */
    const nextMarkers = this.state.markers.filter(marker => marker !== targetMarker);
    this.setState({
      markers: nextMarkers,
    });
  }

  render() {
    return (
      <div style={{height: `100%`}}>
        <Helmet
          title="Getting Started"
        />
        <GettingStartedGoogleMap
          containerElement={
            <div style={{ height: `100%` }} />
          }
          mapElement={
            <div style={{ height: `100%` }} />
          }
          onMapLoad={this.handleMapLoad}
          onMapClick={this.handleMapClick}
          markers={this.state.markers}
          onMarkerRightClick={this.handleMarkerRightClick}
        />
      </div>
    );
  }
}

我也尝试过

const GettingStartedGoogleMap = () => {
    withGoogleMap(props => (
      <GoogleMap
        ref={props.onMapLoad}
        defaultZoom={3}
        defaultCenter={{ lat: 30.3, lng: -97.743 }}
        onClick={props.onMapClick}
      >
        {props.markers.map(marker => (
          <Marker
            {...marker}
            onRightClick={() => props.onMarkerRightClick(marker)}
          />
        ))}
      </GoogleMap>
    ));
}

基于ReactJS giving inst.render is not a function error

代码位于 https://bitbucket.org/codyc54321/anthony-project-react

最佳答案

您需要将高度设为像素

<div style={{height: '200px'}}>
    <Helmet
      title="Getting Started"
    />
    <GettingStartedGoogleMap
      containerElement={
        <div style={{ height: '200px' }} />
      }
      mapElement={
        <div style={{ height: '200px' }} />
      }
      onMapLoad={this.handleMapLoad}
      onMapClick={this.handleMapClick}
      markers={this.state.markers}
      onMarkerRightClick={this.handleMarkerRightClick}
    />
  </div>

关于javascript - 未捕获的 TypeError : inst. 渲染不是函数; react 谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44094243/

相关文章:

javascript - 如何以编程方式调用 Kendo Ui Scheduler 的导航事件

javascript - Node JS - 从异步函数获取数据

javascript - $(...).serializeJSON 不是函数

java - map 上多个标记的实时动画

javascript - 谷歌地图(JavaScript API): get GPS coordinates from address

reactjs - 如何避免在功能组件事件处理程序中绑定(bind)/lambda?

javascript - React - 使用 this.props.children 传递状态

javascript - Google Maps API - 仅限美国的自动完成预测

google-maps - 使用 ID 从 Google Places API 获取位置

reactjs - 使用 React 排序库对 React 元素进行排序