我正在开发一个ReactJS组件,该组件使用Google Maps Javascript API呈现Google地图。在地图上,我动态放置显示不同分销商位置的标记。现在,模拟数据来自具有不同信息的分发器阵列,并且正在循环迭代。单击标记时,我会让InfoWindow显示分发者详细信息。现在在这个InfoWindow中我有一个按钮,允许客户选择这个分销商。
GoogleMap组件归ChooseDistributor组件所有。当单击该按钮时,我想要一个ID(现在是一个带有分发者名称的字符串)通过回调函数发送回ChooseDistributor组件。
问题是当我点击按钮时我得到Uncaught TypeError: Cannot read property 'callbackParent' of undefined
。这可能是一个范围问题,因为当我在循环中时,this
并不是指GoogleMap组件本身(?)。
然后我尝试在循环之外创建一个变量callbackParent
,其中包含this.props.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', 'distributor@company.dk'],
[55.623321, 12.388438, 'Distributor Two', 'Second street 22', '2222 Copenhagen', 'www.company.com', '+4512345678', 'distributor@company.dk'],
[55.670710, 12.389256, 'Distributor Three', 'Third street 33', '3333 Copenhagen', 'www.company.com', '+4512345678', 'distributor@company.dk'],
[55.581179, 12.295583, 'Distributor Four', 'Fourth street 44', '4444 Copenhagen', 'www.company.com', '+4512345678', 'distributor@company.dk'],
[55.647296, 12.284211, 'Distributor Five', 'Fifth street 55', '5555 Copenhagen', 'www.company.com', '+4512345678', 'distributor@company.dk']
]
}
},
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;
更新: 感谢@ Radio-我的问题解决了。解决方案是给每个按钮一个Id,然后通过该Id获取按钮,并像这样添加一个eventlistener:
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));
答案 0 :(得分:0)
&#39; this
&#39;内部callback
方法指的是回调本身。因此,您无法访问this
&#39;内部回调函数引用外部范围。
尝试ES6语法:
myFn:()=>{
this.props.any();
}
,或者
var self = this.
myFn:function(){
self.props.any();
}
答案 1 :(得分:0)
InfoWindow中的按钮不是React组件。它是原始html,可能由setContent
解析。因此,按钮的'onclick'属性将尝试在全局命名空间中执行一个函数。它没有意识到它是如何创建的,callbackParent
,this.props.callbackParent
和locations[i][3]
都不在全局命名空间中供它引用。
您可以在componentDidMount
中使用常规DOM操作来添加事件侦听器。
infoWindow.setContent(... '<button type="button" class="iwButton"><p>Select Distributor</p></button>' : ''));
infoWindow.open(map, marker);
var el = document.getElementsByClassName("iwButton");
el.addEventListener("click", function() { callbackParent(locations[i][3]) });