astroelle
astroelle

Reputation: 1

TypeError: Cannot read property 'map' of undefined on 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>
    )
  }
}

Upvotes: 0

Views: 40

Answers (2)

Sowam
Sowam

Reputation: 1736

Problem

You are passing currentData={this.state.currentData} as a prop but then you are trying to find items const CartItems = ({items}) => {

Solution

You just have to simply change it to items={this.state.currentData}

Upvotes: 1

Viet
Viet

Reputation: 12779

You need fix: <CartItems items={this.state.currentData}/>

Upvotes: 1

Related Questions