Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

A React component crashes while destructuring props or state

ErrorTypeError: 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.

  • Applies to Node.js 22, React 19, JavaScript (ES2022)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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;
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2Cannot destructure ... as it is null.The value is null, so a default does not helpFix
    3. 3The crash happens only on first load and goes away after a refresh or once data arrives.Nothing exists yet to destructure fromFix

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

Did this fix it?

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

Did this fix it?

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.jsx

2Fix

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

Did this fix it?

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:

Related fixes

Web 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 fix

Web 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.