TypeError: Cannot destructure property 'x' of 'undefined' as it is undefined
Filed under Next.js & React errors
A line tried to destructure values from an object that was undefined at that moment — most often data that had not finished loading.
Fastest route
Give your AI this prompt
Copy it, replace the bracketed part with your error, and send it.
Preview the prompt
My app crashes with: TypeError: Cannot destructure property '[property name]' of 'undefined' as it is undefined.
The line it points at is: [paste the destructuring line and a few lines around it].
The value being destructured comes from: [props / a fetch or query hook / context / a function argument — say which, and paste where it is produced if you can].
Please: 1) identify exactly which object is undefined and trace where it should have come from, 2) if it is data that loads asynchronously, add a loading guard so the destructuring only runs when the data exists — do not just hide the crash, 3) if a fallback is appropriate, use the object-level form (value ?? {}), and explain why a property default alone would not have fixed it, 4) if a prop or function argument is missing, fix the call site that should pass it, and show me that call site.
Constraints: do not wrap the line in try/catch, do not change the data the API returns, and do not silence it by deleting the destructuring — I want to know why the value was undefined. Show me each file you change.Fix it yourself
Follow the repair
- Find the exact line and name the undefined thing. The error names the property it failed on, and your browser console or error overlay points at a file and line. Open it and identify the two actors: the property being read (say, name) and the object it is read from (say, user). Everything below is about that object: where does it come from — props, a fetch, context, a function argument — and why is it undefined at that moment?
- If it is loading data: guard the render. The cleanest fix for the most common case is to not reach the destructuring line until the data exists:
With a data-fetching hook the same idea uses its loading state — return the loading UI while the query is pending, and destructure only in the branch where data is present. This is better than sprinkling fallbacks because it also fixes everything else on the page that assumes the data arrived.function Profile({ user }) { if (!user) return <p>Loading profile...</p>; const { name, email } = user; // safe: user exists here return <h1>{name}</h1>; } - Or destructure from a safe fallback. When the component genuinely can render without the object, give the destructuring something harmless to chew on:
Note what this does not do: it makes the crash stop, but name is still undefined — if the page then renders blank values forever, the real problem is upstream (step 5), and the fallback just hid it politely.const { name, email } = user ?? {}; // name and email are simply undefined, no crash // On a function's parameter, the same trick: function connect({ apiKey } = {}) { /* ... */ } - Do not rely on property defaults — they solve a different problem. This line still crashes when user is undefined:
A default like = "Guest" applies when the property is missing from an existing object. When the object itself is missing, you need the object-level guard from steps 2 or 3. This is the single most common failed fix for this error — people add defaults, the error does not move, and they conclude something deeper is broken. It is not; the default was just standing in the wrong place. You can combine both:const { name = "Guest" } = user; // STILL THROWS if user is undefinedconst { name = "Guest" } = user ?? {}; - If the object comes from an API or a parent: check what is actually arriving. Log the value one line before the crash (console.log("user is:", user)) and look at its real shape. Frequent findings: the fetch failed and you are destructuring an error response; the API renamed or nested the field (data.user instead of user); or the parent component passes user under a different prop name than the child destructures, so it arrives as undefined. In the parent case, fix the call site — pass the prop, or rename consistently — rather than adding fallbacks in the child.
- If a function is the victim: fix its call site. When the destructuring is a function's parameter list — function saveSettings({ theme, lang }) — and the crash happens when it is called, some caller is invoking it with no argument (or with an undefined variable). Search the project for the function's name, find the bare call, and pass the object it expects. If being called with nothing is legitimate, add the = {} parameter default from step 3 so the function tolerates it deliberately.
Why this happensOptional explanation
Destructuring is a shortcut for reading several properties at once: const { name, email } = user means "take the name and email properties off user." The shortcut has one hard requirement — there must be an object to take them from. If user is undefined (or null, which produces the twin error naming 'null'), the line cannot even start, and JavaScript reports it by naming the first property it tried to read: "Cannot destructure property 'name' of 'undefined'."
The React-specific reason this is everywhere: components render immediately, but data arrives later. On the first render, your fetched user, your context value, or your props may legitimately be undefined — that is a normal moment in the app's life, not a bug in the data. Code written as if the data is always present survives exactly until the first slow connection. The other breeding ground is function calls: a helper written as function connect({ apiKey }) that some other code calls with no argument at all, often after an AI edit quietly removed the options object from the call site.
- Data has not loaded yet: the component destructures a fetch result or query data that is undefined on the first render.
- A prop was never passed: the parent renders the component without the object the component destructures — often after a rename.
- A function lost its argument: a helper or library call that expects an options object receives nothing.
- The API changed shape: the field you destructure from is nested differently now, or the response is an error object instead of the data.
- The defaults misunderstanding: property defaults were added as the fix and changed nothing, because the whole object is missing.
Quick answers
Why did adding a default value (= 'Guest') not fix it?
No — they guard different things. A property default (= 'Guest') applies when the property is missing from an object that exists. When the whole object is undefined, destructuring cannot even start, defaults included. You need an object-level guard: a loading check before the line, or destructuring from value ?? {}. You can combine both.
It works after a refresh sometimes, then crashes on slow connections. Why?
Most likely the component is rendering before its data arrives — on the first render the fetched value is legitimately undefined, which is normal. Guard with the query's loading state, or render only when data exists. If it also happens long after load, the fetch is probably failing and you are destructuring an error response instead of data.
My error says 'of null' instead of 'of undefined'. Is it different?
Same rule, different wording: the object was null instead of undefined. API responses produce null for empty fields far more often than undefined. The same guards fix both — a null check or the ?? operator, which covers undefined and null together.