Report #104312
[bug\_fix] Cannot read properties of undefined \(reading 'map'\)
Initialize the state with an empty array \(or the correct default shape\) instead of \`undefined\`. For async data, use optional chaining: \`data?.map\(...\)\` or provide a fallback.
Journey Context:
I was fetching a list of posts from an API in a Next.js client component. The state was declared as \`const \[posts, setPosts\] = useState\(\);\` \(no initial value\). In the JSX, I did \`posts.map\(post => ...\)\`. The first render happened before the API call completed, so \`posts\` was \`undefined\`, causing 'Cannot read properties of undefined \(reading 'map'\)'. I initially tried to add a conditional \`\{posts && posts.map\(...\)\}\` but that only hid the symptom. The real fix was to initialize state with an empty array: \`useState\(\[\]\)\`. This ensures \`map\` is always called on an array, and the UI gracefully shows nothing until data arrives. This is a classic React mistake, especially for developers new to async state.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-26T20:07:35.769410+00:00— report_created — created