簡體   English   中英

React-google-maps警告

[英]React-google-maps warnings

我最近開始學習React,我想用Google Maps API構建一個React App,所以我下載了React-google-maps包,但其文檔已過時。 我有此示例代碼。 控制台日志錯誤:

  1. 未定義process.env.ENV,假定為'prod'env
  2. 不推薦通過主React包訪​​問PropTypes
  3. 不推薦通過主React包訪​​問createClass。

TL:DR如何修復此代碼

 import React from "react"; import ReactDOM from "react-dom"; import { withGoogleMap, GoogleMap, Marker, InfoWindow, } from "react-google-maps"; const MapInit = withGoogleMap(props => ( <GoogleMap ref={props.onMapLoad} defaultZoom={1} defaultCenter={{ lat: -25.363882, lng: 131.044922 }} > </GoogleMap> )); class MapContainer extends React.Component { constructor(props) { super(props); this.state = { markers: [ {} ], }; this.handleMapLoad = this.handleMapLoad.bind(this); } handleMapLoad(map) { this._mapComponent = map; } render() { return ( <div style={{height: `100%`}}> <MapInit containerElement={ <div style={{ height: `100%` }} /> } mapElement={ <div style={{ height: `100%` }} /> } onMapLoad={this.handleMapLoad} /> </div> ); } } export default MapContainer; 

它們是由庫生成的,該庫可能未更新為使用class而不是createClass並且正在從React導入PropTypes。

您可以通過瀏覽到node_modules並檢查源代碼來驗證這一點。 有點煩人,但是除了發送拉取請求自己修復之外,您無能為力。 :)

https://github.com/tomchentw/react-google-maps

版本號

  • 對於React >= 15.5 ,在npm上使用next標簽
  • 對於React < 15.5 ,在npm上使用最新標簽

您需要更新到react-google-maps v7.0.0才能擺脫棄用警告。


您可能要考慮改用 google-map-react ,它具有更多的社區活動,並且看起來質量更高。

對於第二次警告,請使用https://www.npmjs.com/package/prop-types

例如import PropTypes from 'prop-types';

第三次使用https://www.npmjs.com/package/create-react-class

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM