Skip to main content

Browser · 6 common causes

is not a function

Updated

You called a method on a value that is not the type you assumed — usually not an array, or undefined.

The exact message

Uncaught TypeError: data.map is not a function
    at ProductList (ProductList.jsx:14:23)
    at renderWithHooks (react-dom.development.js:15486:18)

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is is not a function.

How to fix it

Cause 1 of 6

The value is an object, not an array.

An API returning { items: [...] } gives you an object; the array is one level down, so data.items.map(...) is what you want. console.log(Array.isArray(data), data) settles it in one line.

Cause 2 of 6

The value is undefined or null.

Initialise state as [] rather than leaving it undefined, so the first render maps over an empty array and produces nothing instead of throwing.

Cause 3 of 6

The method does not exist on that type.

A NodeList from querySelectorAll has forEach but no map — convert it with Array.from(nodes). Strings have no push, plain objects have no filter. Check what the value actually is before assuming an array API.

Cause 4 of 6

The import is the wrong shape.

Importing a namespace where a function was expected, or a named export that does not exist, gives you an object or undefined rather than a callable. Check whether the module exports a default, and match the import style.

Cause 5 of 6

JSON round-tripping stripped the prototype.

JSON.parse produces plain objects: a Date comes back as a string and any class instance loses its methods. Rehydrate explicitly — new Date(value), or a factory that rebuilds the instance.

Cause 6 of 6

A name is shadowed or overwritten.

A local variable with the same name as an imported function, or a property assigned over a method, silently replaces it. Search for other assignments to that name in the file.

Stopping it coming back

Type the boundary — TypeScript or a runtime schema check on API responses — so a wrong shape is reported at the fetch rather than at the first method call somewhere deep in a component.

is not a function — common questions

Why does it work sometimes and not others?
The shape differs between responses — an empty result returning null instead of [], or an error body with a different structure. Log the response for the failing case specifically; the successful path is not where the bug is.
How do I turn an array-like into a real array?
Array.from(value) handles NodeLists, Sets, Maps, arguments objects and anything iterable. Spreading with [...value] does the same for iterables but not for plain array-likes without an iterator.
The error names a function I definitely defined. Why?
Either it is defined after use in a way that hoisting does not cover — const declarations are not callable before their line — or a circular import means the module was not finished when you read the binding.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor