Я не могу получить переменную конструктора в моем вызове выборки с помощью javascript и реагировать. Мне нужно значение this.state.numXLabels в обратном вызове .then(function(json), но я получаю TypeError: Cannot read property 'state' of undefined(…). Как правильно это сделать? Вот соответствующий код:
TypeError: Не удается прочитать состояние свойства неопределенного (…)
import React, { Component } from 'react'
class StockGraph extends Component {
constructor(props) {
super(props);
this.state = { numXLabels: 0 }
var url = 'https://www.quandl.com/api/v3/datasets/WIKI/MSFT'+
'.json?api_key=bCRpjzvgPNkxLzqAv2yY';
fetch(url)
.then(function(response) {
return response.json()
})
.then(function(json) {
console.log(this.state.numXLabels);
//this.setState({
// numXLabels: 30
//})
})
}
...