I have this variable on my component's state:
chartData: {
labels:null,
datasets:null
}
I receive this json from my api to create the chart:
{
"labels": [
"13/7",
"14/7",
"15/7"
],
"datasets": [
{
"label": "%%%%",
"data": [
0,
18,
168
],
"backgroundColor": [
"rgba(255, 99, 132, 0.6)",
"rgba(255, 99, 132, 0.6)",
"rgba(255, 99, 132, 0.6)"
]
}
]
}
The chart appears with no problem but when I try to print the value of this.state.chartData this appears:
TypeError: Converting circular structure to JSON --> starting at object with constructor 'HTMLCanvasElement' | property '__reactInternalInstance$khbtn4bp50s' -> object with constructor 'FiberNode' --- property 'stateNode' closes the circle
Here is the setState of the variable:
this.setState({
chartData: {
datasets: response.datasets,
labels:response.labels
}
})
the response is the json above.
UPDATED METHOD JUST TO PRINT VALUES:
teste=()=>{
alert("chart data")
alert(JSON.stringify(this.state.chartData))
}
edit1: print of the console.log(this.state.chartData)
edit 2: api call:
return fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({
labels:params.labels,
datasets:params.datasets
})
}).then(response => response.json())
.then(json => {
console.log("fetchJsonFromApi " + JSON.stringify(json))
// making callback optional
if (callback && typeof callback === "function") {
callback(json);
}
return json;
})
.catch(error => {
console.log(error)
});
}