I can't get a constructor variable within my fetch call using javascript and react. I would like the value of this.state.numXLabels within the .then(function(json) callback, but I get TypeError: Cannot read property 'state' of undefined(…). What is the proper way of doing this? Here is the relevant code:
TypeError: Cannot read property 'state' of undefined(…)
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
//})
})
}
...