Uncaught TypeError: this.state.data.map is not a function

后端 未结 5 1796
無奈伤痛
無奈伤痛 2020-12-15 18:06

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

相关标签:
5条回答
  • 2020-12-15 18:31

    Your initial data state is String., String does not have method .map, you need change your initial state from '' to []

    this.state = {  data: [] };
    
    0 讨论(0)
  • 2020-12-15 18:33

    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

    home_container.js:8 Uncaught TypeError: products.product.map is not a function

    renderItems = (products) => (
                products.product ? 
                products.product.map(item => (
                    <ProductItem {...item} key = { item._id } />
                ))
                : null
            )

    solution

    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.

    0 讨论(0)
  • 2020-12-15 18:42

    If your data is a string type then instead you must use this.state.data.split('').map(ch=>{ console.log('Hello from map') })

    0 讨论(0)
  • 2020-12-15 18:44

    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()
    
    0 讨论(0)
  • 2020-12-15 18:52

    .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.

    0 讨论(0)
提交回复
热议问题