Fix: Cannot read properties of null (reading ‘map’)

React JS map function throwing error cannot read properties of null solution. How to fix map function null error in React JS.

If the array we are going to map is null then such an error may be thrown. Mostly with API calls when there is a delay in getting data.

eg:

Fix: Cannot read properties of null (reading 'map')

We can fix this by adding a condition like if the array exists with data then do the map functions.

We can do that by adding data && data.map()

Fix: Cannot read properties of null (reading 'map')

So this is the solution of Cannot read properties of null error in React JS.

About the Author: smartcoder

You might like

Leave a Reply

Your email address will not be published. Required fields are marked *