在InfoWindow内容中使用React组件

问题描述 投票:1回答:2

目前我正在使用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?

reactjs google-maps infowindow
2个回答
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);

0
投票

React Google Maps是一个很棒的文档库(https://tomchentw.github.io/react-google-maps/

© www.soinside.com 2019 - 2024. All rights reserved.