在没有react-google-map的情况下渲染Google地图

tre*_*ek1 9 google-maps-api-3 reactjs

有没有人能够使用React渲染谷歌地图而不使用react-google-map插件?我正在尝试这样的事情:

var MapTab = React.createClass({

render: function() {

    return  <div className="map-container">
       <div id='map' ></div>
     </div>

},

componentDidMount: function(){

        console.log("Hello")



window.onload = function(){
    (function initMap() {
        var markers = [];
        var geocoder = new google.maps.Geocoder();

        var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 12,
            center: {lat: 37.7749300, lng: -122.4194200}
        });
    })(); 
}


}// end of cdm;
}); 
module.exports = MapTab;
Run Code Online (Sandbox Code Playgroud)

我没有尝试过任何工作.我也尝试使用refs捕获地图,但也没有渲染地图.我已经将google地图脚本放在标题中(使用键),并验证了密钥在vanilla js项目中是有效的.

小智 1

摆脱window.onload。当调用componentDidMount方法时,窗口已经加载,因此您的initMap()函数永远不会触发。