The error
TypeError: Cannot read properties of undefined (reading 'map')
Quick answer:
Find the variable immediately before
.map(). That value is undefined
when React renders the component. If the value
should always be an array, initialize or
normalize it as an array before mapping it.
What this React error means
The JavaScript .map() method
belongs to arrays. This component expected an
array, but the runtime value was undefined.
The important question is not only how to stop the crash. The important question is: why was the value undefined?
The value may be undefined because data has not loaded yet, a prop was omitted, an API response has a different shape, or the component is accessing the wrong nested property.
Common causes
- React state was declared without an initial array value.
- A component rendered before an API request completed.
- The API returned an object, while the component expected an array.
- A required array prop was not passed into the component.
-
The array exists under a nested property such
as
response.data.items, but the component mapsresponse.items. - An unsuccessful API response omitted the expected array.
- State was reset to undefined during another update.
Fix 1: initialize array state correctly
If the state value represents a list, initialize it with an empty array rather than leaving it undefined.
Before
const [items, setItems] = useState();
return (
<div>
{items.map((item) => (
<div key={item.id}>
{item.name}
</div>
))}
</div>
);
After
const [items, setItems] = useState([]);
return (
<div>
{items.map((item) => (
<div key={item.id}>
{item.name}
</div>
))}
</div>
);
Fix 2: validate the API data shape
Initializing state prevents the first-render crash, but the API response must still be validated. Do not assume the returned property is an array.
async function loadItems() {
const response =
await fetch('/api/items');
const result =
await response.json();
const nextItems =
Array.isArray(result.items)
? result.items
: [];
setItems(nextItems);
}
If the actual response stores the array in
result.data.items, use that exact
path instead. Inspect the response before
changing the component.
Fix 3: provide a safe default for props
If a reusable component accepts an array prop, provide a default when an empty list is a valid state.
function ItemList({
items = []
}) {
return (
<div>
{items.map((item) => (
<div key={item.id}>
{item.name}
</div>
))}
</div>
);
}
Fix 4: handle loading and failure states
A loading screen is often clearer than silently rendering an empty list while data is still being fetched.
if (loading) {
return <p>Loading items...</p>;
}
if (error) {
return <p>Unable to load items.</p>;
}
return (
<div>
{items.map((item) => (
<div key={item.id}>
{item.name}
</div>
))}
</div>
);
Do not hide the real problem
Optional chaining can prevent the immediate crash:
{items?.map((item) => (
<div key={item.id}>
{item.name}
</div>
))}
But this is not automatically the correct
repair. If items should never be
undefined, optional chaining can hide a broken
prop, failed request, or incorrect response
path. Diagnose the data source first.
Repair checklist
- Open the first application component in the stack trace.
-
Find the variable immediately before
.map(). - Log or inspect the value and confirm its runtime type.
- Determine whether the value comes from state, props, context, or an API response.
- Verify the precise response or object path.
- Initialize or normalize the value as an array only when an array is the correct data type.
- Retest the first render, successful response, empty response, and failed-request paths.
Still getting the error?
Paste the complete error and stack trace into 0-69. The React repair engine can classify the failure, identify the reported location, and prepare repair steps and copy-ready code for the detected pattern.
Analyze My React ErrorFrequently asked questions
Why does the error happen before the API finishes?
React can render the component before an
asynchronous request completes. If the
component calls .map() during
that first render, the initial value must
already be an array or the component must
intentionally handle the loading state.
Should the initial state be an empty array?
Use an empty array when the state represents a list and an empty list is a valid initial state. Do not use an empty array to disguise a value that should actually be an object, string, or required prop.
What if the value exists but map is not a function?
That is a related but different failure. The variable exists, but it is not an array. Inspect whether the value is an object and locate the nested array that the component should map.