React says .map is not a function on your data
TypeError: results.map is not a functionShort answer
The value you call map on is not an array. It is usually an object that wraps the array, such as { results: [...] }, or the state started as something else. Log it with Array.isArray, read the array from the right property, and start state as an empty array.
Fix it fast
1Find out which cause you have
Print the value and its type right before the map call. This one line tells you which cause below applies.
console.log(results, Array.isArray(results), typeof results);2Match what you see, then jump to its fix
- 1
typeof results is 'object' and Array.isArray is falseThe API returns an object and the array is inside itFix - 2
Array.isArray is false and the object has one key per itemThe value is a plain object whose values you want to listFix - 3
The value has a length but Array.isArray is falseThe value is array-like, such as a NodeListFix - 4
The error happens on the first render, before the request endsState starts as an object or string, not an empty arrayFix
- 1
What does the error look like?
TypeError: results.map is not a functionThe component crashes on render and the page goes blank
Cause 1 of 4
The API returns an object and the array is inside it
1Check
Look at the printed object. Many APIs return something like { count: 20, results: [ ... ] }. The map method only exists on arrays, so calling it on the wrapper throws.
console.log(Object.keys(results));2Fix
Store the inner array in state, not the whole response.
const res = await fetch(url);
const json = await res.json();
setResults(json.results);3Verify
Array.isArray(results) is true and the list renders.
console.log(Array.isArray(results));Confirmed by developer.mozilla.org
Cause 2 of 4
The value is a plain object whose values you want to list
1Check
An object keyed by id, such as { a: {...}, b: {...} }, has no map method.
console.log(Object.keys(results).length);2Fix
Turn the values into an array first with Object.values. Object.values returns only the object's own enumerable values.
Object.values(results).map((item) => (
<li key={item.id}>{item.name}</li>
));3Verify
The list renders one item per key.
console.log(Object.values(results).length);Confirmed by developer.mozilla.org
Cause 3 of 4
The value is array-like, such as a NodeList
1Check
NodeList and other array-like objects have a length and numbered keys but not map.
console.log(results.length, typeof results.map);2Fix
Convert it to a real array with Array.from before mapping. Array.from also takes a mapping function as its second argument.
Array.from(results, (el) => el.value);3Verify
typeof of the converted value's map is 'function'.
console.log(typeof Array.from(results).map);Confirmed by developer.mozilla.org
Cause 4 of 4
State starts as an object or string, not an empty array
1Check
Check the useState call. If it starts as something that is not an array, the first render calls map on that value.
const [results, setResults] = useState({}); // an object on render 12Fix
Start state as an empty array and always set a new array, never a changed old one.
const [results, setResults] = useState([]);
setResults([...results, newItem]);3Verify
The page renders an empty list first and fills in after the data arrives.
console.log(results.length);Confirmed by react.dev
How do I stop it coming back?
- Type the API response so a wrapped object is caught while you type, not in the browser.
- Initialise list state as [] and replace it with a new array on every update.
- Log Array.isArray on the first run against a new API.
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.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
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
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fix