0
votes

import React, { useEffect, useState } from 'react'

export default function Shop() {

useEffect(() => {
    fetchItems();
}, [])

const [items, setItems] = useState([])

const fetchItems = async () => {
    const data = await fetch(
        "https://jsonplaceholder.typicode.com/users"
    );
    const items = await data.json();
    console.log(items.items);
    setItems(items.items)
};
return (
    // <h1>hhh</h1>
    <div>
        {items.map(item => (
                <h1 key={item.itemid}>{item.name}</h1>
        ))}
    </div>
);

}

got stuck here while making project

1

1 Answers

2
votes

const getData = async () => {
const data = await fetch(
        "https://jsonplaceholder.typicode.com/users"
    );
    const items = await data.json();
    console.log(items);
}

getData();

items it is already an array, you can't access items.items. Passing just items to setItems function would be enough.