I am using a class component. When I run this script, it plots the graphic, but in the next line when it calls the function computePM_from_ClickedPoint, I get the error below. The error refers to “this”, but without it, I cannot call the function. Suggestions?
Error: Cannot read properties of undefined (reading 'computePM_from_ClickedPoint')
JavaScript
x
52
52
1
2
interface IProps {
3
mapView: __esri.MapView | __esri.SceneView;
4
}
5
6
constructor(props) {
7
super(props);
8
this.enableMapClick = this.enableMapClick.bind(this);
9
}
10
11
enableMapClick = () => {
12
var view = this.props.mapView;
13
14
view.on("click", function (evt) {
15
var point = new Point({
16
longitude: evt.mapPoint.longitude,
17
latitude: evt.mapPoint.latitude,
18
spatialReference: view.spatialReference,
19
});
20
21
var pointMarker = new PictureMarkerSymbol({
22
url: "https://static.arcgis.com/images/Symbols/Animated/EnlargeRotatingRedMarkerSymbol.png",
23
height: "20px",
24
width: "20px",
25
});
26
27
var graphic = new Graphic({
28
geometry: point,
29
symbol: pointMarker,
30
});
31
32
console.log(graphic)
33
34
view.graphics.add(graphic);
35
this.computePM_from_ClickedPoint(graphic.geometry);
36
});
37
38
computePM_from_ClickedPoint(screenPoint) {
39
var mp = webMercatorUtils.webMercatorToGeographic(screenPoint, false);
40
----
41
-----
42
}
43
44
render() {
45
return (
46
<div>
47
<Button onClick={this.enableMapClick}>
48
Click the Map
49
</Button>
50
</div>
51
)}
52
tried unsuccessfully changing the call to:
JavaScript
1
2
1
this.computePM_from_ClickedPoint(graphic.geometry).bind(this);
2
Advertisement
Answer
Instead of bindingthis.computePM_from_ClickedPoint(graphic.geometry).bind(this)
try to bind to this.enableMapClick
on onClick
event (this way you can call it with this/Class same specific object), something like.
JavaScript
1
6
1
render() { return (
2
<div>
3
<button onClick="{this.enableMapClick.bind(this)}">Click the Map</button>
4
</div>
5
)}
6
For more details:- javascript-bind-method