React: too many re-renders when using useState in a loop

poze 2 months ago271 gadeen

2

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?

Dyagnostik IA

Se IA ki fè l. Se pa yon repons — kominote a anba a konfime oswa korije l. Toujou verifye anvan ou fye l.

poze 2 months ago

2 Repons

2
✓
✓ Repons yo aksepte

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.

Konekte pou di lòt moun si li te mache.

answered 2 months ago
1

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.

Konekte pou di lòt moun si li te mache.

answered 2 months ago

Sign in and verify your email to post an answer.