React: too many re-renders when using useState in a loop
asked 19 days ago221 viewsen
I call setState inside a .map() and React throws "Too many re-renders". What's the correct pattern to compute derived state from an array?
🤖 AI diagnosis
AI-generated. Not an answer — the community below confirms or corrects it. Always verify before relying on it.
2 Answers
An ORM with parameterized queries prevents injection for values. You still must: validate/whitelist any dynamic identifiers (table/column names), enforce input schemas (e.g. Zod), and apply least-privilege DB roles. Never string-concatenate SQL.
Sign in to tell others whether this worked.
Don't call setState in map. Compute the derived value directly:
const total = items.reduce((s, i) => s + i.price, 0);
If you truly need state, set it once in an effect with the computed result, not per iteration.
Sign in to tell others whether this worked.
Sign in and verify your email to post an answer.