我正在使用從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