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>
)
}
>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