微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

导出地图组件时如何解决打字稿错误

如何解决导出地图组件时如何解决打字稿错误

我在使用 GoogleApiWrapper 导出地图组件时遇到打字稿错误。它工作正常,我没有将它包装在 GoogleApiWrapper 中,我尝试了多种解决方案,但没有人为我工作。任何人请查看代码并提供任何建议。遇到这个问题,当我用必要的道具调用这个组件时,我遇到了道具问题。

问题可能与将我的组件包装在 GoogleApiWrapper 中有关。

import { GoogleApiWrapper,Map,Marker } from "google-maps-react";
import React from "react";
import "./GoogleMap.css";
import Autocomplete from 'react-google-autocomplete';
import {connect} from 'react-redux'
export interface GoogleMapProps {
    google?: any | undefined,position: any,mapStyles?:any,mapColor?:any,handleSelect:(place: any)=>void,onAddressChange: (address: any) => void;
    countryID:string,}
const GoogleMap: React.FC<GoogleMapProps> = (props:GoogleMapProps ) => {
    const {position,mapStyles,mapColor,handleSelect,countryID}=props;
    const renderMap = () => {
        return (
            <div>
                <div style={{ width: '100%',height: '270px' }}>
                    {
                        <Map
                            google={props.google}
                            initialCenter={position}
                            center={position}
                            zoomControl={false}
                            mapTypeControl={false}
                            style={mapStyles}
                            scaleControl={false}
                            streetViewControl={false}
                            fullscreenControl={false}
                            containerStyle={{
                                position: 'absolute',width: '95%',height: '40%',borderRadius: '12px',}}
                            onReady={(mapProps,map) => {
                                console.log("Map is ready")
                            }}
                            styles={mapColor}
                        >
                            <Marker
                                position={position}
                            />
                        </Map>
                    }
                </div>
                <Autocomplete
                    className={"autocomplete_input"}
                    style={{ width: '100%' }}
                    placeholder={"Type your address"}
                    onPlaceSelected={(place: any) => {
                        handleSelect(place)
                    }}
                    types={['(regions)']}
                    componentRestrictions={{ country: countryID }}
                />
                <svg className="svg_icon" width="23" height="25" viewBox="0 0 23 25" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path d="M22.5789 20.7516L18.3102 16.0781C18.1175 15.8672 17.8563 15.75 17.5823 15.75H16.8844C18.0661 14.0953 18.7683 12.0141 18.7683 9.75C18.7683 4.36406 14.7822 0 9.86273 0C4.94326 0 0.957153 4.36406 0.957153 9.75C0.957153 15.1359 4.94326 19.5 9.86273 19.5C11.9307 19.5 13.8317 18.7313 15.3431 17.4375V18.2016C15.3431 18.5016 15.4501 18.7875 15.6428 18.9984L19.9115 23.6719C20.3139 24.1125 20.9647 24.1125 21.3629 23.6719L22.5746 22.3453C22.9771 21.9047 22.9771 21.1922 22.5789 20.7516ZM9.86273 15.75C6.83569 15.75 4.38238 13.0688 4.38238 9.75C4.38238 6.43594 6.83141 3.75 9.86273 3.75C12.8898 3.75 15.3431 6.43125 15.3431 9.75C15.3431 13.0641 12.8941 15.75 9.86273 15.75Z" fill="#96A7AF" />
                </svg>
            </div>
        )
    }
    return (
        <React.Fragment>
            {renderMap()}
        </React.Fragment>
    )
}
export default GoogleApiWrapper(
    () => ({
      apiKey: "....key here...."
    }
  ))(GoogleMap)

解决方法

google 上有很多关于 React 高阶组件的资料,like this for example

这是我的建议,试试看是否有效(至少 TS 编译器没有抱怨任何事情):

// make 'google' not optional to satisfy props for wrapper 
export interface GoogleMapProps {
    google: any,position: any,mapStyles?:any,mapColor?:any,handleSelect:(place: any)=>void,onAddressChange: (address: any) => void;
    countryID:string,}

...

// compose components like this:
export default GoogleApiWrapper(({apiKey: "....key here...."}))<GoogleMapProps>((props) => (<GoogleMap {...props} />));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?