I'm having some trouble understanding the scope of this
inside of ES6 classes. Here, I have component:
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
credentials: {
v: '3.exp',
key: null,
},
coordinates: {
lat: 51.5258541,
lng: -0.08040660000006028,
},
};
Meteor.call('googleMapsApiKey', (err, res) => {
if (!err) {
this.setState({ key: res });
}
});
}
onMapCreated(map) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(() => {
this.setState({
coordinates: {
lat: this.lat,
lng: this.lng,
},
});
});
} else {
console.log('geolocation not supported');
}
map.setOptions({
disableDefaultUI: true,
});
}
...
I get a error on line 33, which is in the onMapCreated(map)
function, specifically the call to this.setState
.
I suspect I'm missing sort of binding, or that the scope of this refers to navigator.geolocation
and therefore no longer references the class. What do you guys think?