Maaz Zindani

Loading the index…

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

React says .map is not a function on your data

ErrorTypeError: results.map is not a function

Short 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.

  • Applies to React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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);
  2. 2Match what you see, then jump to its fix

    1. 1typeof results is 'object' and Array.isArray is falseThe API returns an object and the array is inside itFix
    2. 2Array.isArray is false and the object has one key per itemThe value is a plain object whose values you want to listFix
    3. 3The value has a length but Array.isArray is falseThe value is array-like, such as a NodeListFix
    4. 4The error happens on the first render, before the request endsState starts as an object or string, not an empty arrayFix

What does the error look like?

  • TypeError: results.map is not a function
  • The 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

Did this fix it?

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

Did this fix it?

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

Did this fix it?

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 1

2Fix

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

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.