Maaz Zindani

Loading the index…

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

TypeError: undefined is not iterable when destructuring a hook or array

ErrorTypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))

Short answer

Array destructuring, spread, for...of and new Set all need an iterable. The error means the value on the right is undefined, null, an object, a Promise or an uncalled function. Log that value, then return an array, call the function, await the Promise, or use a fallback.

  • 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

    Find the line that has [a, b] = on the left or ... or for...of in it, and print the value on its right side with its type.

    const result = useCounter();
    console.log(typeof result, result);
    const [value, setValue] = result;
  2. 2Match what you see, then jump to its fix

    1. 1undefined is not iterable (cannot read property Symbol(Symbol.iterator))The source is undefined or nullFix
    2. 2object is not iterableA hook or function returns an object, and you destructure it as an arrayFix
    3. 3The right side is a function name without parentheses, or the result of an async call.The function was not called, or a Promise was not awaitedFix
    4. 4x is not iterable on a for...of loop or [...x]A plain object is used with for...of or spread into an arrayFix

What does the error look like?

  • TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))
  • TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator))
  • TypeError: x is not iterable

Cause 1 of 4

The source is undefined or null

1Check

A context value, an API result or a function without a return is undefined. A destructuring default only covers single items, not the whole source.

console.log(maybeList);

2Fix

Give the source a fallback before destructuring.

const list = maybeList ?? [];
const [first = null] = list;

3Verify

Run the code path that failed and confirm that it no longer throws.

console.log(first);

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 4

A hook or function returns an object, and you destructure it as an array

1Check

Array destructuring needs an iterable, so a custom hook that returns { value, setValue } fails when called as [value, setValue].

grep -n "return" src/hooks/useCounter.js

2Fix

Return an array from the hook, or destructure the result as an object.

// option 1: return an array
function useCounter() {
  let value = 0;
  return [value, (v) => { value = v; }];
}
// option 2: keep the object and destructure by name
const { value, setValue } = useCounter();

3Verify

Render the component again.

npm run dev

Confirmed by developer.mozilla.org

Did this fix it?

Cause 3 of 4

The function was not called, or a Promise was not awaited

1Check

A hook name without () is a function, and the result of an async function is a Promise. Neither is iterable.

grep -n "= useState;" src/

2Fix

Call the function. For a Promise, await it inside an async function.

const [state, setState] = useState(0);
const [a, b] = await fetchPair();

3Verify

Run the code again and check that the values have the types you expect.

console.log(a, b);

Confirmed by developer.mozilla.org

Did this fix it?

Cause 4 of 4

A plain object is used with for...of or spread into an array

1Check

Plain objects have no iterator. Spreading one into an array or looping over it with for...of fails.

console.log(Array.isArray(data));

2Fix

Loop over Object.entries, Object.keys or Object.values instead.

for (const [country, capital] of Object.entries(obj)) {
  console.log(country, capital);
}

3Verify

Run the loop and confirm each pair prints.

console.log(Object.entries(obj));

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Pick one return shape for a custom hook and keep it: array for positional use, object for named use.
  • Normalise API data once, with ?? [], where it enters the component.
  • Log the value and its type before spreading or destructuring when a new error appears.

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.