在InfoWindow内容中使用React组件

时间:2018-02-28 01:42:18

标签: reactjs google-maps infowindow

目前,我正在使用google-maps api开发页面 我在实施InfoWindow时遇到了问题 我想在React Component的内容中使用InfoWindow,如下所示..

const infoWindow = new google.maps.InfoWindow({
    content : <MyComponent />,
    position : { lat : ..., lng : ... }
})

infoWindow.open(map);  

实际上,我使用了renderToString,如下面的

const infoWindow = new google.maps.InfoWindow({
    content : renderToString(<MyComponent />),
    position : { lat : ..., lng : ... }
})

infoWindow.open(map);

非常好。但是我无法使用<MyComponent />中的任何函数,例如lifecycle api ..

那么,在InfoWindow中使用React Component是否有好的方法?

2 个答案:

答案 0 :(得分:0)

我解决了以下问题,但我不确定并且有点接线...

const myComponent = <MyComponent />;
const infoWindow = new google.maps.InfoWindow({
    content : renderToString(myComponent),
    position : { lat : ..., lng : ... }
})

infoWindow.open(map);

setTimeout(() => {
   ReactDOM.render(myComponent , $('.gm-style-iw > div > div').get(0));
}, 100);

答案 1 :(得分:0)

React Google Maps是一个出色的图书馆,文档很棒(https://tomchentw.github.io/react-google-maps/