我正在使用从Google Maps JavaScript API呈现Google地图的ReactJS组件。在地图上,我动态地放置显示不同分销商位置的标记。现在,模拟数据来自一系列具有不同信息的分销商,并且正在循环迭代。当点击一个标记时,我有InfoWindow显示发布者的详细信息。现在在这个InfoWindow中,我有一个允许客户选择这个分销商的按钮。ReactJS组件将数据传回父组件不起作用
GoogleMap组件归ChooseDistributor组件所有。当点击按钮时,我想要一个ID(它现在是一个带有分配器名称的字符串)通过回调函数发送回ChooseDistributor组件。
问题是我点击按钮时得到一个Uncaught TypeError: Cannot read property 'callbackParent' of undefined
。这可能是一个范围问题,因为this
在我处于循环(?)中时并未引用GoogleMap组件本身。
我试图在包含this.props.callbackParent
的循环之外创建一个变量callbackParent
,该变量应该链接回ChooseDistributor组件中的接收函数。然后我在按钮onclick上调用这个变量。这不起作用,因为我得到一个Uncaught ReferenceError: callbackParent is not defined
。
如果有人对此问题有可能的解决方案,我将不胜感激。我错过了什么,或者它是一个纯粹的语法错误?提前致谢。
以下是我的代码。
ChooseDistributor组件
var ChooseDistributor = React.createClass({
getSelectedDistributor: function(company){
alert(company);
},
render: function(){
return(
<div>
<Header headerClass="title" title="Choose distributor"/>
<div className="fillViewPort">
<GoogleMap enableIwSelectButton={true} callbackParent={this.getSelectedDistributor}/>
</div>
<NavBar />
</div>
);
}
})
module.exports = ChooseDistributor;
GoogleMap的组件:
var GoogleMap = React.createClass({
getInitialState: function(){
return {
// LOCATIONS STATE SHOULD GET DATA FROM STORE
locations: [
// ARRAY VALUES: [0]=LAT, [1]=LNG, [2]=COMPANY, [3]=ADDRESS, [4]=CITY, [5]=WEBSITE, [6]=PHONE, [7]=EMAIL
[55.628353, 12.388910, 'Distributor One', 'First street 11', '1111 Copenhagen', 'www.company.com', '+4512345678', '[email protected]'],
[55.623321, 12.388438, 'Distributor Two', 'Second street 22', '2222 Copenhagen', 'www.company.com', '+4512345678', '[email protected]'],
[55.670710, 12.389256, 'Distributor Three', 'Third street 33', '3333 Copenhagen', 'www.company.com', '+4512345678', '[email protected]'],
[55.581179, 12.295583, 'Distributor Four', 'Fourth street 44', '4444 Copenhagen', 'www.company.com', '+4512345678', '[email protected]'],
[55.647296, 12.284211, 'Distributor Five', 'Fifth street 55', '5555 Copenhagen', 'www.company.com', '+4512345678', '[email protected]']
]
}
},
componentDidMount: function(){
var locations = this.state.locations;
var enableIwSelectButton = this.props.enableIwSelectButton;
var callbackParent = this.props.callbackParent;
var map = new google.maps.Map(document.getElementById('googleMap'), {
zoom: 11,
center: new google.maps.LatLng(locations[0][0], locations[0][1]), // CENTER PROPERTY SHOULD GET COORDINATES OF THE DEVICE CURRENT LOCATION IF PERMITTED.
mapTypeControlOptions: {
mapTypeIds: [google.maps.MapTypeId.ROADMAP]
},
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true
});
var infoWindow = new google.maps.InfoWindow();
var marker, i;
// PLACE MARKER ON MAP FOR EACH DISTRIBUTOR POSITION IN ARRAY.
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][0], locations[i][1]),
map: map
});
// ADD INFOWINDOW WITH DISTRIBUTOR DETAILS TO EACH MARKER.
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infoWindow.setContent('<p class="iwTitle">' + locations[i][2] + '</p>'+
'<p class="iwText">' + locations[i][3] + '<br>' +
locations[i][4] + '<br>' +
'<a href="http://' + locations[i][5] + '">' + locations[i][5] + '</a></p>' +
'<p class="iwText"><i class="fa fa-phone iwIcons"></i>' + locations[i][6] + '<br>' +
'<i class="fa fa-envelope iwIcons"></i><a href="mailto:"' + locations[i][7] + '>' + locations[i][7] + '</a></p>' +
(enableIwSelectButton ? '<button type="button" class="iwButton" onclick={this.props.callbackParent(locations[i][3])}><p>Select Distributor</p></button>' : ''));
infoWindow.open(map, marker);
}
})(marker, i));
}
},
render: function(){
return(
<div id="googleMap" className="size-100-pct" />
);
}
})
module.exports = GoogleMap;
UPDATE: 感谢@广播网 - 我的问题得到解决。该解决方案是给每个按钮的ID,然后通过该标识后取按钮,添加一个事件侦听到这样的:
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infoWindow.setContent('<p class="iwTitle">' + locations[i][2] + '</p>'+
'<p class="iwText">' + locations[i][3] + '<br>' +
locations[i][4] + '<br>' +
'<a href="http://' + locations[i][5] + '">' + locations[i][5] + '</a></p>' +
'<p class="iwText"><i class="fa fa-phone iwIcons"></i>' + locations[i][6] + '<br>' +
'<i class="fa fa-envelope iwIcons"></i><a href="mailto:"' + locations[i][7] + '>' + locations[i][7] + '</a></p>' +
(enableIwSelectButton ? '<button type="button" id="btnSelect' + i + '" class="iwButton"><p>Select Distributor</p></button>' : ''));
infoWindow.open(map, marker);
var el = document.getElementById("btnSelect" + i);
el.addEventListener("click", function(){callbackParent(locations[i][2])});
}
})(marker, i));
您是否尝试将此绑定到父组件? ' ' –
somallg
@somallg是的,我试过了。不工作。它给出了一个'警告:绑定():你正在绑定一个组件方法。 React以高性能的方式自动执行此操作,因此您可以安全地删除此呼叫。' – RonRonDK