Fix: data.map is not a function in React js

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Almost 2-3 hrs i wasted my time to solving this error and I find this solution in your video thanks ❤

damteimran
Автор

I still could not solvw the problem even after going through your video:
my implemenation:
import { useEffect, useState } from 'react';
import { Row, Col } from 'react-bootstrap';
import Product from '../components/Product';
import axios from 'axios';

const HomeScreen = () => {
const [products, setProducts] = useState([]);

useEffect(() => {
const fetchProducts = async () => {
try {
const { data } = await axios.get('/api/products');
setProducts(data);
} catch (error) {
console.error('Error fetching products:', error);
}
};

fetchProducts();
}, []);

return (
<>
<h1>Latest Products</h1>
<Row>
{Array.isArray(products) &&
products?.map((product) => (
<Col key={product._id} sm={12} md={6} lg={4} xl={3}>
<Product product={product} />
</Col>
))}
</Row>
</>
);
};

export default HomeScreen;

AnthonyObi-wrro