3 回答

TA贡献1876条经验 获得超7个赞
如果有人想要一种功能组件方式来实现这一点并让它识别道具,只需参考包装器。
import { useState } from 'react';
import PlacesAutocomplete from 'react-places-autocomplete';
import { GoogleApiWrapper, IProvidedProps } from 'google-maps-react';
interface PlacesAutocompleteProps {
value: string;
disabled?: boolean;
onChange: () => void;
counrtyResctrictions?: string[];
}
const PlacesAutoComplete = ({
onChange,
value,
disabled,
counrtyResctrictions = [],
}: PlacesAutocompleteProps & IProvidedProps) => {
return (
<PlacesAutocomplete>
</PlacesAutocomplete>
);
};
export default GoogleApiWrapper({
apiKey: process.env.REACT_APP_FB_API_KEY!,
language: 'en',
})<PlacesAutocompleteProps & IProvidedProps>(PlacesAutoComplete);

TA贡献2080条经验 获得超4个赞
我将粘贴适用于 .TSX 的部分代码
class App extends React.Component<any, any> {
constructor(props: any) {
super(props);
// ...
}
}
export default GoogleApiWrapper(
(props: any) => ({
apiKey: <your_key>
}
))(App)

TA贡献1772条经验 获得超8个赞
老实说,我没有太大变化。我主要添加了它要求的 <{google}> 参数,它似乎正在寻找它丢失的那个道具。我正在使用 Visual Studio Code,它有时也会为我缓慢地处理错误,这可能会首先说明为什么会出现问题。
import React, {Component} from 'react'
import {Map, InfoWindow, Marker, GoogleApiWrapper, mapEventHandler, markerEventHandler} from 'google-maps-react';
import { coordinates } from '../customerPage'
const mapStyle = {
width: '920px',
height: '500px'
}
export class MapContainer extends Component<{google}>{
onMapClicked: mapEventHandler;
onMarkerClick: markerEventHandler;
onInfoWindowClose: any;
map?: google.maps.Map | google.maps.StreetViewPanorama
render(){
return(
<>
<Map google={google}
zoom={16}
draggable
initialCenter={{
lat: coordinates.latitude,
lng: coordinates.longitude
}}
onReady={(mapProps, map) => {
this.setState({ map: map as google.maps.Map})
}}
style={mapStyle}
onClick={this.onMapClicked}>
<Marker onClick={this.onMarkerClick}
title={`Location of ...`} />
</Map>
<p className="float-left md:ml-0 mt-64 lg:ml-48 sm:pt-64 lg:pt-64">
<button className="bg-white hover:bg-gray-200 text-gray-800 font-semibold py-3 px-5 border border-gray-400 rounded shadow">Alarm</button>
<button className="bg-white hover:bg-gray-200 text-gray-800 font-semibold py-3 px-5 border border-gray-400 xs:ml-20 sm:ml-20 md:ml-32 lg:ml-40 rounded shadow">Unlock</button>
<button className="bg-white hover:bg-gray-200 text-gray-800 font-semibold py-3 px-5 border border-gray-400 xs:ml-20 sm:ml-20 md:ml-32 lg:ml-40 rounded shadow">Reset</button>
</p>
</>
)
}
}
const GoogleMap = GoogleApiWrapper({
apiKey: 'xxx'
})(MapContainer)
export default GoogleMap;
添加回答
举报