2016-01-14 26 views
0

我已經reactjs模式彈出功能,我怎麼能說reactjs功能上的谷歌地圖標記onClick事件

getModal: function() { 
return (
    <Modal> 
    <ModalBody> 
     <p>One fine body&hellip;</p> 
    </ModalBody> 
    </Modal> 
); 

},

所以我怎麼能調用getModal功能在谷歌地圖標記onClick事件,這裏是一個代碼。 this.getModal() - 不起作用...所以如何獲得這個功能。

componentDidMount: function() { 
    function placeMarker(location) { 
    var addMarker = new google.maps.Marker({ 
     position: location, 
     map: map, 
     draggable: true, 
     icon: "/imgs/marker.png" 
    }); 

    google.maps.event.addListener(addMarker, 'click', function(){ 
     this.getModal(); 
    }); 
    } 
} 

enter image description here

回答

0

事件處理程序在執行不同的時間範圍內(this)。你可以將其綁定到正確的上下文,如:

google.maps.event.addListener(addMarker, 'click', (function(){ 
    this.getModal(); 
}).bind(this)); 

或:

google.maps.event.addListener(addMarker, 'click', this.getModal.bind(this)); 
+0

它仍然沒有作品 – user3844078

+0

這麼多分享你的代碼。 – madox2

+0

正如我可以看到你從'getModal'函數返回反應元素,但它不是React的工作方式。您應該在事件處理程序中設置狀態,並在'render'方法中渲染模態。在這裏瞭解更多https://facebook.github.io/react/docs/component-api.html – madox2

0

要知道你的情況下,即,「這個」寫JavaScript時的意義。它在使用API​​和框架(例如GMaps和jQuery)時始終在變化。

在你的情況,你將需要引用有getModal()對象在其範圍內,所以:something.getModal();

相關問題