I folowed tthis page: https://blog.logrocket.com/using-chart-js-react/ The data is loaded into the data and I see in console but the data is not loaded into the useState
import React, { useEffect, useState } from "react";
import budgetApi from "../api/budgetApi";
import { Bar } from "react-chartjs-2";
const BarChart = () => {
const [chartData, setChartData] = useState({});
useEffect(() => {
getDataFromChart();
}, []);
const getDataFromChart = async () => {
try {
const response = await budgetApi.getByCategory();
const data = response.data;
setChartData({
labels: [data.map((budget) => budget.category)],
datasets: [
{
label: "Forint",
data: [data.map((budget) => budget.total_amount)],
backgroundColor: ["#ffbb11", "#ecf0f1", "#50AF95"],
borderWidth: 1,
},
],
});
console.log(chartData); //Empty!
} catch (error) {
console.log(error);
}
};
return (
<div>
{
<Bar
data={chartData}
options={{
plugins: {
title: {
display: true,
text: "Cryptocurrency prices",
},
legend: {
display: true,
position: "bottom",
},
},
}}
/>
}
</div>
);
};
export default BarChart;
I get some errors for example: can't access property "map", nextDatasets is undefined I think I get it because the chartData is empty.
Please help. Thanks for the answers