3
I have the following data set:
const dados = [5, 10, 15, 20, 25, 50, 90];
const colors = [
"#52DF9A",
"#FFCE1C",
"#3570BD",
"#3570BD",
"#00B894",
"#FB6B32",
];
And I’ve got the following state:
const [data, setData] = useState([{}]);
I need to go through the data and put a data with a respective color, so that the "given" state is as follows:
data = [
{ valor: 5, color: "#52DF9A" },
{ valor: 10, color: "#FFCE1C" },
// ...
];
I did it this way with useEffect
:
useEffect(() => {
dados.forEach((dado, i) => {
setData((prev) => {
return [...prev, { valor: dado, color: colors[i] }];
});
});
}, []);
But the problem is, when I do it this way, the first position is occupied by an empty object ({}
), and I need the data to start at first position. How can I solve this?
'Cause if I do this:
const [data, setData] = useState([]);
Makes a mistake.
In theory it was not to
const [data, setData] = useState([])
give error. I could say in more detail what happens?– Luiz Felipe
When I put only [ ] it says this: Argument of type '(Prev: Never[]) => { value: number; color: string; }[]' is not Assignable to Parameter of type 'Setstateaction<Never[]>'.
– Rindi
If you want to understand why this happens, you can open a another question. Just don’t forget to create a [mcve].
– Luiz Felipe