Are you running into the frustrating “TypeError: Cannot Read Property ‘Map’ of Undefined” error in your
React
application? This error can be tricky to debug, but fear not – we’ve got you covered.
In this article, we’ll walk you through the common causes and solutions to help you fix this error. Whether you’re a seasoned React
developer
or just starting out, this guide will help you get your app back on track.
What Causes the “TypeError: Cannot Read Property ‘Map’ of Undefined” Error?
The “TypeError: Cannot Read Property ‘Map’ of Undefined” error typically occurs when you try to access a property or method of an undefined value in your React code.
In plain terms, the error occurs when you try to map over an undefined value, such as an array that has not been initialized or has not yet received data.
In the example below, you are getting todo items from
JSON Placeholder
data, but the map method is called before the data from an
API
request has arrived.
The code above will throw the “TypeError: Cannot read properties of undefined (reading ‘map’)” error:
TypeError: Cannot read properties of undefined (reading ‘map’) error message
You need to look for a way to let
React
know that the todos state is an array even before the array gets populated, or you need to avoid the map method from running until the todos state variable gets its data from the API request.
3 Ways To Fix the “TypeError: Cannot Read Property ‘Map’ of Undefined” Error
Here are three ways to fix the “TypeError: Cannot Read Property ‘Map’ of Undefined” error in React:
Initialize your state variable to an empty array
Use comparison operators
Use the optional chaining operator (?.)
Let’s explore each of these solutions and how they can help you resolve the error in your React code.
1. Initialize Your State Variable to an Empty Array
One of the direct solutions to the “TypeError: Cannot Read Property ‘Map’ of Undefined” error is to ensure that the array variable you’re trying to map over is defined.
You can initialize your state variable to an empty array by default, which will ensure that the variable always exists and doesn’t throw an error when you try to map over it.
For example, the following are two similar components, the first’s state variable is not initialized to an empty array, while the second is initialized:
// Before initializing your state variable to an empty array
function MyComponent() {
const [myList, setMyList] = useState();
return (
{myList.map(item => <li>{item}</li>)}
// After initializing your state variable to an empty array
function MyComponent() {
const [myList, setMyList] = useState([]);
return (
{myList.map(item => <li>{item}</li>)}
In the example above, myList state variable is initialized to an empty array by default using useState([]). This ensures that even if myList is undefined initially, it will always be an array and won’t throw the “TypeError: Cannot Read Property ‘Map’ of Undefined” error.
For the fetch example, you can also initialize the todos state variable to an empty array ([]):
import { useState, useEffect } from 'react';
function App() {
// Initialize the state to an empty array of todos.
const [todos, setTodos] = useState([]);
useEffect(() => {
const getTodos = async () => {
const response = await fetch(
'https://jsonplaceholder.typicode.com/todos?_limit=5'
const data = await response.json();
setTodos(data);
getTodos();
}, []);
console.log(todos);
return (
{todos.map((todo) => (
<div key={todo.id}>
<h2>Item: {todo.title}</h2>
export default App;