Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

phones.map is not a function REACTJS

I am using the . map method and i am getting phones.map is not a function error .

Below is my code

import { Link } from "react-router-dom";
import {React , useState , useEffect} from 'react'
import Axios from 'axios';
export default function Phones() {
  const [phones,setPhones] = useState([])
  useEffect(() => {
    Axios.get("https://dummyjson.com/products/search?q=phone")
    .then(res => {      
      setPhones(res.data)      
    }).catch(err => console.log(err))
  },[])
  const display_phones = phones.map(item => <h1> {item.name} </h1>)


  return(
  <div className="Phones">
      {display_phones}

    </div>
  
  )
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Because you get an object that has an array of products and you already have to map it.

{products: Array(4), total: 4, skip: 0, limit: 4}

Try the following:

const display_phones = phones.products.map(item => <h1> {item.title} </h1>)

FYI Your object does not have a key like name

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading