importReact,{useState,useEffect}from"react";importChartfrom"react-google-charts";importapifrom'~/services/api';import{Container}from'./styles';import{startOfDay,endOfDay}from'date-fns';// put your independant functions outside your componentsconstsum=(a,b)=>a+b;constuniqueCategories=(array)=>[...newSet(array.map(item=>item.category))];functionProducts(){const[chartdata1,setChartdata1]=useState([["Category","value"],["Categoria1",16.6]])useEffect(()=>{asyncfunctionloadchart(){constdate1=startOfDay(newDate())constdate2=endOfDay(newDate())constresponse=awaitapi.get('sales',{params:{date1,date2}});constapiResult=[{name:"test1",category:"Categoria1",price:3},{name:"test2",category:"Categoria1",price:13.6},{name:"test3",category:"Categoria2",price:8},{name:"test4",category:"Categoria2",price:8}];constchartData=uniqueCategories(apiResult).map(category=>[category,apiResult.filter(item=>item.category===category).map(item=>item.price).reduce(sum)]);setChartdata1([["Category","value"],...chartData]);}loadchart();},[]);
import React, { useState, useEffect } from "react";
import Chart from "react-google-charts";
import api from '~/services/api';
import { Container } from './styles';
import { startOfDay, endOfDay } from 'date-fns';
function Products() {
const [chartdata1, setChartdata1] = useState([["Category","value"],["Categoria1", 16.6]])
useEffect(() => {
async function loadchart() {
const date1 = startOfDay(new Date())
const date2 = endOfDay(new Date())
const response = await api.get('sales', {
params: { date1, date2 }
});
}, []);
I'm sorry, I don't know how to post the code correctly.
Thank you very much for the help you are giving me
just do
setChartdata1(chartData)I had done it, really, and it hadn't worked.
Now it worked.
Thank you very much, I don't know how to thank you. I've been in this for 2 days.
Thank you