我正在开发一个基于地图的应用程序,该应用程序使用 Google Map API 在 React.js 中创建标记及其信息窗口。The infowindow.setContent()
only 接受 aString
或HTML
。我不可能传入,String
因为我有一个button
链接到另一个 react 组件中的特定方法(例如:)_this.props.addList(place)
。因此,我必须将参数作为 HTML DOM 填充为以下代码行:
var div = document.createElement('div');
var title = document.createElement('h4');
title.innerHTML = place.name;
var btn = document.createElement('button');
btn.className = 'btn btn-danger btn-block';
btn.appendChild(document.createTextNode('I want to go here !!'));
div.appendChild(title).appendChild(btn);
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent( div );
infowindow.open(map, this);
});
btn.addEventListener('click', function() {
_this.props.addList(place);
});
这些代码对我有用,但我不想一个一个地创建元素。我也试过用 React 组件传递参数,但它似乎不起作用:
createMarker: function() {
/** Some other lines of code */
var _this = this;
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent( _this._renderInfoWindow(place) );
infowindow.open(map, _this);
});
},
// my infowindow rendering method
_renderInfoWindow: function(place) {
return(
<div>
<h4>{place.name}</h4>
<p>{place.cost}</p>
<button className="btn btn-danger btn-block" onClick={this.props.addList.bind(this, place)}>I want to go here !! </button>
</div>
)
},
那么有没有另一种方法至少将一个react组件转换为HTML,这样我就不必document.createElement()
一个一个地写了?
谢谢