A React component crashes while destructuring props or state
TypeError: Cannot destructure property 'name' of 'undefined' as it is undefined.Short answer
JavaScript throws this when the right side of a destructuring pattern is undefined or null. In React that is usually a prop the parent did not pass, data that has not loaded yet, or a hook or context that returns nothing. Log the value being destructured, then give it a default or guard it.
Fix it fast
1Find out which cause you have
The error names the property but not the variable. Log the object right before the line that destructures it, and check whether it is undefined or null.
console.log('props:', props); const { user } = props;2Match what you see, then jump to its fix
- 1
The crash happens on the first render of a child, and the parent JSX has no attribute for that prop.The component is rendered without the propFix - 2
Cannot destructure ... as it is null.The value is null, so a default does not helpFix - 3
The crash happens only on first load and goes away after a refresh or once data arrives.Nothing exists yet to destructure fromFix
- 1
What does the error look like?
TypeError: Cannot destructure property 'name' of 'undefined' as it is undefined.TypeError: Cannot destructure property 'b' of 'null' as it is null.TypeError: Cannot destructure 'null' as it is null.
Cause 1 of 3
The component is rendered without the prop
1Check
Look at where the component is used. Destructuring a whole parameter fails when nothing is passed in its place.
grep -rn "<Profile" src/2Fix
Give the whole parameter a default object, and give each property its own default. The whole-parameter default only applies when the argument is undefined.
function Profile({ name = 'Guest', age = 0 } = {}) {
return <p>{name} ({age})</p>;
}3Verify
Render the component with no props and confirm that the page loads.
<Profile />Confirmed by developer.mozilla.org
Cause 2 of 3
The value is null, so a default does not help
1Check
An API or a state variable often holds null while data loads. Defaults apply only to undefined, never to null.
console.log(user === null, user === undefined);2Fix
Turn null into an empty object with the nullish operator before destructuring.
const { name = 'Guest' } = user ?? {};3Verify
Set the value to null in a test render and confirm there is no crash.
<Profile user={null} />Confirmed by developer.mozilla.org
Cause 3 of 3
Nothing exists yet to destructure from
1Check
Find the line that reads the object. It runs before the data is ready, so the object is still undefined.
grep -n "const {" src/components/Profile.jsx2Fix
Check the value before using it, and return early while it is missing.
if (user === undefined || user === null) {
return <p>Loading...</p>;
}
const { name } = user;3Verify
Reload the page with a slow network and confirm that the placeholder shows first.
console.log('rendered with', user);Confirmed by developer.mozilla.org
How do I stop it coming back?
- Give destructured function parameters a default of {} when callers may omit them.
- Treat data from an API as possibly null and normalise it once, near the place it enters your component.
- Keep the shape of a prop the same on every call site.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
A React component crashes calling .map on undefined
TypeError: Cannot read properties of null (reading 'map')
The value you call .map on is undefined or null when React renders.
3 causes, each with a fixWeb apps
Jest fails to parse a React Native or Expo dependency
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax
Jest does not run Babel on node_modules by default, and many React Native packages ship untranspiled source.
3 causes, each with a fixWeb apps
Sentry: Non-Error promise rejection captured with value in a React app
Non-Error exception (or promise rejection) captured with keys: x, y, z
Sentry raises this event when something that is not an Error object is thrown or used to reject a promise, for example a plain object from an API.
3 causes, each with a fixWeb apps
TypeScript says a JavaScript module implicitly has an any type
Could not find a declaration file for module 'react-materialize'. 'path/to/module-name.js' implicitly has an 'any' type.
TypeScript found the JavaScript file but no types for it, and noImplicitAny turns that into an error.
2 causes, each with a fixWeb apps
TypeError: undefined is not iterable when destructuring a hook or array
TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator))
Array destructuring, spread, for...of and new Set all need an iterable.
4 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fix