javascriptreactjsmaterial-uiarrayiterator

Can't we iterate material-ui inside an array in React.js?


I have been trying to use material-ui and iterate over it inside an array ( for creating ratings for some items like in e-commerce sites). The code isn't working. On my localhost server, it's not showing any stars at all. Before I made it dynamic, it was working all fine, then I added props to my functional component as per to make it dynamic. Everything else is working just fine except that it's not accepting my matrial-ui icon inside the array for me to iterate over. Moreover, the import statement says "it's value is never read although it's imported"

My code: Product.js:

import React from "react";
import "./Product.css";
import StarRateIcon from "@material-ui/icons/StarRate";

function Product({ id, title, image, price, rating }) {
  return (
    <div className="product">
      <div className="product_info">
        <p>{title}</p>
        <p className="product_price">
          <small>$</small>
          <strong>{price}</strong>
        </p>
        <div className="product_rating">
          {Array(rating)
            .fill()
            .map((_, i) => (
              <p StarRateIcon className="star" />
            ))}
        </div>
      </div>
      <img src={image} alt="" />
      <button>Add to Basket</button>
    </div>
  );
}

export default Product;

My home.js file :

import React from "react";
import "./Home.css";
import Product from "./Product";

function Home() {
  return (
    <div classname="home">
      <div className="home_container">
        <img
          className="home_image"
          src="https://m.media-amazon.com/images/G/01/digital/video/sonata/US3P_JOKER_IMAGE_ID/be07783e-2738-4aaf-b90c-d0ec474d15ae._UR3000,600_SX1500_FMwebp_.jpg"
        />
        <div className="home_row">
          <Product
            id="890778"
            title="Description"
            price={99.99}
            image="https://m.media-amazon.com/images/I/71BGLa7IFtL._AC_UY218_.jpg"
            rating={5}
          />
          <Product />
        </div>
      </div>
    </div>
  );
}

export default Home;

Help Somebody! I can use emoji snippets but I really want this to work. I have imported StarRateIcon and used it. It isn't working.


Solution

  • Looks like you accidentally put a 'p' in front of your icon component name. You have <p StarRateIcon className="star" /> when it should be <StarRateIcon className="star" />