Next.js Discord

Discord Forum

Cannot iterate over states

Unanswered
Swedish Lapphund posted this in #help-forum
Open in Discord
Swedish LapphundOP
const [finishedFlags, setFinishedFlags] = React.useState<boolean[]>(() => steps.map(() => false));

const completedMainSteps = () => {
    return finishedFlags.filter(flag => flag).length;
};

4 Replies

Swedish LapphundOP
gives me TypeError: finishedFlags.filter is not a function
however when I had: const finishedFlags = React.useState<boolean[]>(() => steps.map(() => false)); the same code works.
when doing: console.log(finishedFlags) I get: Array(6) [ false, false, false, false, false, false]. But for whatever reason I cannot perform a simple .ForEach on it, nor can I iterate over it with a simple for loop as it claims to be "not iteratable"?
surprisingly when doing: console.log(typeof finishedFlags); it gives me boolean indicating its a singlular boolean, but when printing the value it shows me the above. Not sure why it's like this.