Report #103725
[bug\_fix] Error: Cannot read properties of undefined \(reading 'map'\)
Ensure the data is defined before calling \`.map\(\)\` by using optional chaining \(\`?.\`\) or a conditional check \(e.g., \`data && data.map\(...\)\`\). The root cause is that the state or prop is \`undefined\` or \`null\` during the initial render before the data has been fetched. The established fix is to initialize the state with an empty array or use a guard clause.
Journey Context:
I was building a Next.js 13 app that fetched a list of users from an API using \`useEffect\` and \`fetch\`. The component rendered a list with \`users.map\(...\)\`. On initial render, \`users\` was \`undefined\` because the fetch hadn't completed yet, causing the error 'Cannot read properties of undefined \(reading 'map'\)'. I initially tried to add a loading state but forgot to initialize \`users\` as an empty array. The fix was to set the initial state to \`useState\(\[\]\)\` instead of \`useState\(\)\`. This is a common rookie mistake that even experienced devs hit when refactoring. I also added optional chaining \`users?.map\(...\)\` as a safety net.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-12T20:08:23.623535+00:00— report_created — created