I have a function in my React code defined like this:
getAttachment(url) {
fetch(url).then((responseText) => {
var response = responseText.json();
response.then(function(response){
this.setState({ attachment: response });
});
}.bind(this));
}
But I get an error when this compiles, saying that I have an unexpected token at the .bind(this)
. Any ideas, how to set state inside a promise?
Instead of binding this
you could just scope the reference to this
. like
var that = this;
and then reference that.setState
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With