Maaz Zindani

Loading the index…

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

React: TypeError, invalid attempt to destructure non-iterable instance

ErrorTypeError: Invalid attempt to destructure non-iterable instance

Short answer

Square brackets destructuring needs an iterable such as an array. This error means the value on the right is an object, undefined or null. Use curly braces for objects, check what your hook or context really returns, and give async data an empty array fallback.

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

Fix it fast

  1. 1Find out which cause you have

    Log the value on the right-hand side of the failing const [...] line. Its type tells you which cause applies.

    const result = useMyHook();
    console.log(typeof result, Array.isArray(result), result);
  2. 2Match what you see, then jump to its fix

    1. 1typeof result is "object" and Array.isArray(result) is falseA hook or context returns an object but you destructure it as an arrayFix
    2. 2typeof result is "undefined"The value is undefined because the context has no valueFix
    3. 3The error appears on first load and the value is undefined or nullAsync data is not there yet on the first renderFix

What does the error look like?

  • TypeError: Invalid attempt to destructure non-iterable instance
  • TypeError: 'x' is not iterable

Cause 1 of 3

A hook or context returns an object but you destructure it as an array

1Check

Array destructuring reads values by position through the iterable protocol, and a plain object is not iterable. Look at the return statement of the hook, or the value passed to the context provider.

grep -rn "return {" src/hooks/

2Fix

Destructure by name with curly braces, or change the hook to return an array if you want the useState style.

// hook returns { user, login }
const { user, login } = useAuth();   // not [user, login]

3Verify

The component renders and the logged value is no longer an object error.

npm run dev

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 3

The value is undefined because the context has no value

1Check

useContext returns the default value passed to createContext when no provider is above. A provider with no value prop gives undefined instead. Check the provider tree and its value prop.

grep -rn "createContext\|Context.Provider\|<.*Context value" src/

2Fix

Wrap the component in the provider, pass value={...} to it, and move the provider above the component that calls useContext.

<AuthContext value={[user, setUser]}>
  <App />
</AuthContext>

3Verify

useContext in the child now logs an array.

npm run dev

Confirmed by react.dev

Did this fix it?

Cause 3 of 3

Async data is not there yet on the first render

1Check

Destructuring undefined or null throws in both array and object form. API data is undefined until the request finishes.

grep -rn "const \[.*\] = .*data" src/

2Fix

Give the value a fallback before destructuring, or render a loading state until it exists.

const [first = null] = data ?? [];
const { name } = user ?? {};

3Verify

Reload the page; first paint no longer throws.

npm run dev

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Document what each custom hook returns, and keep array returns to two-value hooks like useState.
  • Type your hooks in TypeScript so the wrong destructuring is a compile error.
  • Always render a loading state before using data from a request.

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

Next step

Want this handled for good?

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