When I try to run this code it gives me this error:
× TypeError: Cannot read properties of undefined (reading 'map')
I don't know why. I have tried possible means, but it didn't work.
import React from 'react';
import Grid from '@material-ui/core/Grid';
import Product from './Product/Product';
import useStyles from './styles';
const products = [
{id: 1, name: 'Shoes', description: 'Running Shoes.' },
{id: 2, name: 'MacBook', description: 'Apple MacBook.' },
];
const Products = ({ products }) => {
const classes = useStyles();
return (
<main className={classes.content}>
<div className={classes.toolbar} />
<Grid container justify="center" spacing={4}>
{products.map((products) => (
<Grid item key={product.id} item xs={12} sm={6} md={4} lg={3}>
<Product />
</Grid>
))};
</Grid>
</main>
);
};
export default Products;
Check INITIAL_STATE in the reducer.
You must specify an array in the ID object you are calling in the reducer.
Example:
const INTIAL_STATE = {
products: [],
product: { name: "", brand_name: "", prices: "", color: "", images: []},
error: "",
}
product.images.map(img => {
return(
<div key = {img.id} className = "img-itemm">
<img src = {img.image} alt = {img.image} />
</div>
)
})
Make sure to pass the products properties into the Product component. Change this:
{products.map((products) => (
<Grid item key={product.id} item xs={12} sm={6} md={4} lg={3}>
<Product/>
to this:
{products.map((products) => (
<Grid item key={products.id} item xs={12} sm={6} md={4} lg={3}>
<Product/>
Why? Because you're mapping an object (products) into a JSX element in your case which is the <Product/> component, so using { product.id } you're trying map an undefined object. Then make sure the products property you're trying to map out, is correctly passed into the parent component using the Products component.
It's because you have taken the array "products" and the map item "products" by the same name. Change the map item to something else like "product":
const products = [
{id: 1, name: 'Shoes', description: 'Running Shoes.' },
{id: 2, name: 'MacBook', description: 'Apple MacBook.' },
];
const Products = ({ products }) => {
const classes = useStyles();
return (
<main className={classes.content}>
<div className={classes.toolbar} />
<Grid container justify="center" spacing={4}>
{products.map((product) => (
<Grid item key={product.id} item xs={12} sm={6} md={4} lg={3}>
<Product/>
</Grid>
))};
</Grid>
</main>
);
};
There is a property "products" in your component. That variable has higher priority than the map you have outside, and your .map is using it. I would recommend to rename one of them, to avoid variables with the same name.
Given the error, I would guess that that property wasn't passed to the component.
Also, the parameter of the map lambda is "products" too. Change it to "product", or it will fail.
The properties, products, that you're passing to your component (Products) are undefined. The Map method is taking in account the products that you have passed as properties is not the one that you have created outside the component itself.
If you want to map out the products array that you created outside of your components then just change its name as the array has the same name as the properties passed. If you want to use the products (from the property) then make sure that you're passing the properties in the component.
{products && products.map((product) => (
<Grid item key={product.id} item xs={12} sm={6} md={4} lg={3}>
<Product />
</Grid>
))};
You had name each iteration of the map method with products but you are using product to catch each instance of the iteration.
Sometimes, It gives an error when you haven't used the variable of useState. and I have made 2 other components. 1) Tour 2) Tours
const [tours, setTours ] = useState([]);
Now, I have used this tours variable in App.js file using Tours components.
I have used tours in App.js file.
Now, I'm taking tours variable for using .map() function in Tours.js file.
const Tours = ({ tours }) => {
/...
{tours.map()
.../
You have to check that both files have same spelling otherwise It gives an error and also doen't work UI of that particular file.
Thanks for reading.
I had the same error and solved it by first asking if the array existed.
Example:
<Filter>
{ product.color?.map((c) => (
<FilterColor color = {c} key = {c} />
))};
</Filter>
at first please check that is products exported if yes then
change
{products.map((products) => (
<Grid item key={product.id} item xs={12} sm={6} md={4} lg={3}>
<Product/>
to
{products.map((products) => (
<Grid item key={products.id} item xs={12} sm={6} md={4} lg={3}>
<Product/>
because here you map products as products so you have to set key like products.id
you should try this :
{products.map((product) => (
<Grid item key={product.id} item xs={12} sm={6} md={4} lg={3}>
<Product/>
</Grid>
))};
step 1 : Add loading into user page use useState
{const [loading, setLoading] = useState(true);}
step 2 : set loading value to false at the end of async fetch function if your using try catch method this code line put inside the try method
{ setLoading(false)}
step 3 : use a unary operation in the inside the render() part of the code
{ loading ? (
<h2>loading</h2>
) : {/* `enter code here */}}