Hey, in my componentWillMount method, I had to use the trick of
let that = this;
in order to be able to update my state, my question is: is there a better way to do it without having to explicitly create that? Similar to how I don’t have to bind(this)
when using arrow functions.
Just in case, here is my code:
componentWillMount() {
let that = this;
navigator.geolocation.getCurrentPosition(function(location) {
fetchWeather(location.coords.latitude, location.coords.longitude)
.then(data => {
that.setState({
data
})
console.log(data);
})
});
}
I have tried with
navigator.geolocation.getCurrentPosition = location => {
fetchWeather(location.coords.latitude, location.coords.longitude)
.then(data => {
this.setState({
data
})
console.log(data);
})
}
But, it doesn’t even make it to send the request, so my question pretty much is, how to rewrite:
navigator.geolocation.getCurrentPosition(function(location) {
})
As an arrow function.
Thanks!