TypeError: Cannot read property ‘map’ of undefined on reactJS

  javascript, reactjs

I had to do something similar to this before (displaying items in a grid) so I tried to follow that pattern. I have stared at this for a while trying to figure out what is wrong. Is there something I’m not seeing? Help

Error message

   9 |   console.log(localStorage.getItem("shoppingCart")),
  10 | <div>
  11 |   <Grid container spacing={3}>
> 12 |   {items.map((item) => (
  13 |       <Grid item key={item.VinylID} xs={12} sm={4} >
  14 |           <CartLayout item={item} />
  15 |         {/* <cartLayout

CartItems.js

const CartItems = ({items}) => {
  const classes = useStyles();
  return (
      console.log(localStorage.getItem("shoppingCart")),
    <div>
      <Grid container spacing={3}>
      {items.map((item) => (
          <Grid item key={item.VinylID} xs={12} sm={4} >
              <CartLayout item={item} />
            {/* <cartLayout
              item={lineItem}
              onUpdateCartQty={onUpdateCartQty}
              onRemoveFromCart={onRemoveFromCart}
            /> */}
          </Grid>
        ))}

CartLayout.js

const cartLayout = ({ item }) => {
  const classes = useStyles();
  return (
    <Card className="cart-item">
      {/* <CardMedia/> */}
      <CardContent className={classes.cardContent}>
        <Typography variant="h4"> {item.Name} </Typography>
        <Typography variant="h5"> ${item.VinylPrice} </Typography>
      </CardContent>
      <CardActions className={classes.cartActions}> 
      <div className={classes.buttons}>
          <Button variant="contained" type="button" color="secondary">
              Remove
          </Button>
      </div>
      </CardActions>
    </Card>
  );
};

export default cartLayout;

Cart.js

  constructor(props) {
    super(props);
    this.state = {
      currentData: JSON.parse(localStorage.getItem("shoppingCart")).items,
    };
  }

  render() {
    return (
      <div>
        <CartItems currentData={this.state.currentData}/>
      </div>
    )
  }
}

Source: Ask Javascript Questions

LEAVE A COMMENT