Skip to content
Advertisement

How to make child components use map() from App.js props data?

My react is 18.2. I want the child component to receive data and use map() from App.js.

I checked the child component received the data, but I don’t know why does it can’t use map().

It shows this error.

Uncaught TypeError: Cannot read properties of undefined (reading 'map')

So do anyone can fix it? I guess this is render problem, but I don’t know how to fix it, thank you.

fetched():

JavaScript

App.js:

JavaScript

Child.js:

JavaScript

Advertisement

Answer

Since the data coming from your fetch is an array of objects, you may want to initialize the state as an empty array useState([]), useEffect will fire once the component has been mounted so at first, toChild will be undefined and that’s why you are getting that error. So basically:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement