Skip to main content

Browser · 6 common causes

Cannot read properties of undefined

Updated

You read a property from a value that is undefined — the object you expected was never assigned.

The exact message

Uncaught TypeError: Cannot read properties of undefined (reading 'name')
    at UserCard (UserCard.jsx:7:22)
    at renderWithHooks (react-dom.development.js:15486:18)
    at mountIndeterminateComponent (react-dom.development.js:20103:13)

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is Cannot read properties of undefined.

How to fix it

Cause 1 of 6

The data has not arrived yet on the first render.

Render for the loading state explicitly: if (!user) return <Spinner />; before touching any field. Optional chaining with a default — user?.name ?? '' — works for a single value, but an early return is clearer when the whole component depends on the data.

Cause 2 of 6

A lookup returned nothing.

array.find(...) returns undefined when no element matches, and an out-of-range index does the same. Store the result, check it, then use it — const match = items.find(...); if (!match) return null;.

Cause 3 of 6

The response shape differs from what you assumed.

Log the raw response before destructuring it. A payload wrapped as { data: { user } }, or an error body returned with a 200 status, is the usual difference — and the field name in the error message tells you which level went missing.

Cause 4 of 6

A nested destructure hit a missing intermediate.

const { profile: { email } } = user throws when profile is absent. Destructure one level at a time, or supply a default: const { profile = {} } = user.

Cause 5 of 6

this is undefined inside a callback.

A class method passed directly as an event handler loses its receiver, so this.state is undefined. Define it as a class-property arrow function, or bind it in the constructor.

Cause 6 of 6

An import is undefined at runtime.

Importing a default that does not exist, or naming a named export incorrectly, yields undefined rather than an error — the failure surfaces at the first property access. Check whether the module exports a default or named bindings.

Stopping it coming back

Give every state variable a sensible initial value rather than leaving it undefined, and put the API response through a schema check at the boundary so a shape change fails with a useful message.

Cannot read properties of undefined — common questions

What is the difference between undefined and null here?
The message names which one you hit: undefined usually means a variable was never assigned or a lookup missed, null means something deliberately set it to nothing. Optional chaining short-circuits on both.
Is optional chaining everywhere a good idea?
No. It is right where a value is legitimately optional, and wrong where it hides a bug — a?.b?.c?.d silently produces undefined and moves the failure somewhere less obvious. Use it at genuine optional boundaries and check explicitly elsewhere.
The stack trace points at minified code. How do I read it?
Enable source maps for the build and the browser will map frames back to your source. Most bundlers emit them for production if asked; you can upload them to an error tracker rather than serving them publicly.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor