I am new to React, have seen some of the similar issues, but didn’t find why this happens. I am getting an “Uncaught TypeError: this.state.data.map is not a function”. Here
Your initial data
state is String
., String
does not have method .map
, you need change your initial state from ''
to []
this.state = { data: [] };
I have the same error in react-redux.
problem was when I add new product in add/product page and click on the back button then the component of the home page is showing this error
renderItems = (products) => (
products.product ?
products.product.map(item => (
<ProductItem {...item} key = { item._id } />
))
: null
)
I am passing {} instead of [] from the node server
So, when you have this type of error in react-redux then first check what you are passing from the server into that field and also check reducers of that particular component.
If your data is a string type then instead you must use
this.state.data.split('').map(ch=>{ console.log('Hello from map') })
First you must split data to an array, then you may use the map function. Instead of
this.state.data.map
use
this.state.data.split('').map()
.map
is not applicable to a String object. Consider changing 'data' to an array. .map
is a method that calls a function on every element of an array.