Fetching API from fetch in React
Mon Nov 06 2023 20:57:00 GMT+0000 (Coordinated Universal Time)
Saved by
@kalpitsahu
function App() {
const [product, setProduct] = useState([]);
useEffect(() => {
const apiURL = "https://fakestoreapi.com/products";
fetch(apiURL)
.then((response) => {
if (!response.ok) {
throw new Error("No Response from the API");
}
return response.json();
})
.then((data) => {
setProduct(data);
})
.catch((error) => {
console.error("Not Feteched");
});
}, []);
return (
<div>
<ul>
{product.map((product) => (
<li>{product.title}</li>
))}
</ul>
</div>
);
}
export default App;
content_copyCOPY
Comments