TypeError: undefined is not iterable when destructuring a hook or array
TypeError: 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.
Fix it fast
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;2Match what you see, then jump to its fix
- 1
undefined is not iterable (cannot read property Symbol(Symbol.iterator))The source is undefined or nullFix - 2
object is not iterableA hook or function returns an object, and you destructure it as an arrayFix - 3
The 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
x is not iterable on a for...of loop or [...x]A plain object is used with for...of or spread into an arrayFix
- 1
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
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.js2Fix
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 devConfirmed by developer.mozilla.org
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
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
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:
- 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 while destructuring props or state
TypeError: Cannot destructure property 'b' of 'null' as it is null.
JavaScript throws this when the right side of a destructuring pattern is undefined or null.
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
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 fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fix