I am new to React, I dont really know how React hook works, here I encounter a problem. I tried to draw a chart using Chart.js and re draw it in specific interval using data from API, the problem is the chart does not read the data. This is the get data function:
const getData = async ()=>{
const response = await axiosJWT.get('http://localhost:5000/in-total-assets', {
headers:{
Authorization: `Bearer ${token}`
}
})
var formated = [];
formated.push = response.data[0].Total_OR_Weight;
formated.push = response.data[0].Total_IN_Weight;
setData(formated)
}
And this is the chart renderer function:
async function getChart(){
await getData();
let config = {
type: "doughnut",
data: {
labels: [
"Organic",
"Inorganic",
],
datasets: [
{
label: 'Dataset',
backgroundColor: ["#ed64a6", "#4c51bf"],
data: data,
hoverOffset: 5
},
],
}
};
let ctx = document.getElementById("doughnut-chart").getContext("2d");
window.myBar = new Chart(ctx, config);
}
And here is the useEffect function:
React.useEffect(() => {
refreshToken();
getChart();
const id = setInterval(getChart, 10000)
return ()=> clearInterval(id);
}, []);
The result is the chart show undefined as the value of the chart, any idea how to solve it?