I am quite new with react and am probably making a silly mistake. I am trying to make an api call with axios that returns a promise. When this promise resolves I would like to pass the result of this to a function that updates state on the parent through a callback. However it appears that 'this' is gone as I get undefined. I guess as its resolved in the future there is no longer a 'this' context? I can get around it by assigning the callback to a temp and use that but it feels clumsy. Here is the code:
axios.get(url)
.then(function(response) {
this.props.handler(response.data.thing)
})
.catch(function(error) {
console.log(error)
})
this works:
let handler = this.props.handler
axios.get(url)
.then(function(response) {
handler(response.data.word)
})
.catch(function(error) {
console.log(error)
})