React: TypeError, invalid attempt to destructure non-iterable instance
TypeError: Invalid attempt to destructure non-iterable instanceShort 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.
Fix it fast
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);2Match what you see, then jump to its fix
- 1
typeof result is "object" and Array.isArray(result) is falseA hook or context returns an object but you destructure it as an arrayFix - 2
typeof result is "undefined"The value is undefined because the context has no valueFix - 3
The error appears on first load and the value is undefined or nullAsync data is not there yet on the first renderFix
- 1
What does the error look like?
TypeError: Invalid attempt to destructure non-iterable instanceTypeError: '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 devConfirmed by developer.mozilla.org
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 devConfirmed by react.dev
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 devConfirmed by developer.mozilla.org
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:
- 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
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 fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fixWeb apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fixWeb apps
An axios call never finishes when the API server stops answering
code: 'ECONNABORTED'
Axios has no timeout unless you give it one: the default is 0, which means wait forever.
3 causes, each with a fix