Sto tentando di aggiornare lo stato da promise
che ho ricevuto utilizzando la funzione fetch
.Impossibile impostareState dalla funzione then della Promessa
componentDidMount(){
fetch(url).then((responseText) => {
var response = responseText.json();
response.then(function(response){
this.setState(response);
});
});
}
stavo ottenendo l'errore che il setState
non è una funzione
Poi, ho cercato di bind(this)
per passare il valore this
come qui di seguito.
componentDidMount(){
fetch(url).then((responseText) => {
var response = responseText.json();
response.then(function(response){
this.setState(response);
});
}).bind(this);
}
Non funziona anche ora. Lo stesso errore di nuovo.
Trova ironicamente questo metodo leggibile. – ApertureSecurity