简体   繁体   English

Google-Maps-React:将道具传递给 App.js 的问题

[英]Google-Maps-React: Problems passing props to App.js

I'm working on a website and I'd like to use google maps API via Google-Maps-React package.我在一个网站上工作,我想通过 Google-Maps-React 包使用谷歌地图 API。 I've tested it before, as a single application, and I successfully manage to get data back from the API and render it on screen.我之前已经将它作为单个应用程序进行了测试,并且我成功地设法从 API 取回数据并将其呈现在屏幕上。 Now I'm trying to use it on my actual project and it is not working.现在我试图在我的实际项目中使用它,但它不起作用。 I imagine that the issue might be that I'm not passing props properly from a component to another.我想问题可能是我没有正确地将道具从一个组件传递到另一个组件。

Any suggestions on how to fix this issue would be very much appreciated.非常感谢有关如何解决此问题的任何建议。 Thank you very much!非常感谢!

Here are my two components and the message I'm getting on the console (Webpack compiled successfully) :这是我的两个组件以及我在控制台上收到的消息(Webpack 编译成功):

index.js:139 Uncaught Error: You must include a google prop at new Map (index.js:139) index.js:139 未捕获的错误:您必须在新地图中包含一个google道具 (index.js:139)

Venue.js holds the actual map Venue.js保存实际地图

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

const mapStyles = {
    width: '100%',
    heigth: '100%',
};

export class MapContainer extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            venues: [
                { lat: 52.471015, lng: 12.719707 }
            ]
        }
    }

    displayMarkers = () => {
        return this.state.venues.map((venue, index) => {
          return <Marker key={index} id={index} position={{
            lat: venue.latitude,
            lng: venue.longitude
          }}
          onClick={() => console.log(this.state.venues)} />
        })
      }

      render() {
        return (
          <Map
           google={this.props.google}
           zoom={8}
           style={mapStyles}
           initialCenter={{ lat: 52.471015, lng: 12.719707 }}
          >
            {this.displayMarkers()}

          </Map>
        );
      }
    }


export default GoogleApiWrapper({
    apiKey:''
  })(MapContainer);

App.js应用程序.js

import React from "react";
import { BrowserRouter, Route, Link } from "react-router-dom";
import Main from "./main";
import MapContainer from "google-maps-react";


export class App extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            error: false,
        };        
    }

    componentDidMount() {
        console.log("App mounted");

    }

    render() {
        return (
            <BrowserRouter>
                <div>
                    <Route exact path="/" component={Main} />
                    <Route exact path="/venue" component={MapContainer} />
                </div>
            </BrowserRouter>
        );
    }
}

Because your import MapContainer path is incorrect !因为你的导入 MapContainer 路径不正确!

in your App.js file change :在您的App.js文件中更改:

import MapContainer from "google-maps-react";

to your MapContainer component like :到您的 MapContainer 组件,如:

import MapContainer from "./components/Venue.js";

simple demo : HERE简单演示:这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM