Product List λ¦¬νŒ©ν† λ§ πŸ”§

Saemsol YooΒ·2021λ…„ 2μ›” 20일
0

μ‚Όν•­μ—°μ‚°μž 쀑첩 지양!

productCadr.js

  • μ‚Όν•­μ—°μ‚°μžμ•ˆμ—μ„œ 또 μ‚Όν•­μ—°μ‚°μžλ₯Ό μ¨λ²„λ €μ„œ μ½”λ“œκ°€λ…μ„±μ΄ λ„ˆλ¬΄ λ–¨μ–΄μ Έ 버렸닀..

μ½”λ“œ μˆ˜μ • μ „

   {item.stock === 0 ? (
    <button disabled type="button">
     Out of stock
    </button>
   ) : typeof item.displayPrice === 'string' ? (
    <button type="button">Select</button>
   ) : (
    <button type="button">Add</button>
   )}

μ½”λ“œ μˆ˜μ • ν›„ ✨

   {item.stock === 0 ? (
     <button disabled type="button">
      Out of stock
     </button>
    ) : (
     <button type="button">
      {typeof item.displayPrice === 'string' ? 'Select' : 'Add'}
     </button>
    )}



map ν•¨μˆ˜μ—μ„œ return 전에 쑰건뢀 λ Œλ”λ§ ν•΄μ£ΌκΈ°

productCadr.js

  • μœ„μ˜ μ½”λ“œμ—μ„œ item.stock κ³Ό item.displayPrice 에 κ΄€ν•΄ 쑰건을 λ”°μ§€λŠ” λ‘œμ§μ„ mapν•¨μˆ˜ μ•ˆμ—μ„œ return 전에 λ‹€λ‘œ μž‘μ„±ν•΄μ„œ 쑰건뢀 λ Œλ”λ§μœΌλ‘œ κ΅¬ν˜„ν•΄λ³΄κΈ°!!!

μ½”λ“œ μˆ˜μ • μ „

 {cardInfo.item.map(item => {
          return (
            <div
              className="productCard"
              key={item.id}
              onClick={() => console.log(item)}
            >
              <div className="productCard__header">
                {item.isNew && <span className="new">NEW</span>}
                <img src={item.imageUrl} alt="product image" />
                <div className="symbols">
                  {item.symbolUrl.map(symbolSrc => {
                    return <img src={symbolSrc} alt="" />;
                  })}
                </div>
              </div>
              <div className="productCard__text-block">
                <h2>{item.displayTitle}</h2>
                <p>{item.subtitle}</p>
                <ul>
                  {item.description.map(text => {
                    return <li>{text}</li>;
                  })}
                </ul>
              </div>
              <div className="productCard__bottom">
                <p>${item.displayPrice}</p>
                {item.stock === 0 ? (
                  <button disabled type="button">
                    Out of stock
                  </button>
                ) : (
                  <button type="button">
                    {typeof item.displayPrice === 'string' ? 'Select' : 'Add'}
                  </button>
                )}
              </div>
            </div>
          );
        })}

μ½”λ“œ μˆ˜μ • ν›„ ✨
β†’ πŸ€” μ΄λ ‡κ²Œ ν•˜λŠ”κ²Œ λ§žλŠ”μ§€ 잘 λͺ¨λ₯΄κ² λ‹€ γ…Žγ…Ž

 {cardInfo.item.map(item => {
          const outOfStock = item.stock === 0;
          const isPriceTypeString = typeof item.displayPrice === 'string';

          return (
            <div
              className="productCard"
              key={item.id}
              onClick={() => console.log(item)}
            >
              <div className="productCard__header">
                {item.isNew && <span className="new">NEW</span>}
                <img src={item.imageUrl} alt="product image" />
                <div className="symbols">
                  {item.symbolUrl.map(symbolSrc => {
                    return <img src={symbolSrc} alt="" />;
                  })}
                </div>
              </div>
              <div className="productCard__text-block">
                <h2>{item.displayTitle}</h2>
                <p>{item.subtitle}</p>
                <ul>
                  {item.description.map(text => {
                    return <li>{text}</li>;
                  })}
                </ul>
              </div>
              <div className="productCard__bottom">
                <p>${item.displayPrice}</p>
                {outOfStock ? (
                  <button disabled type="button">
                    Out of stock
                  </button>
                ) : (
                  <button type="button">
                    {isPriceTypeString ? 'Select' : 'Add'}
                  </button>
                )}
              </div>
            </div>
          );
        })}



React children μ‚¬μš©ν•΄λ³΄κΈ°

productCadr.js

  • 같은 λ ˆμ΄μ•„μ›ƒ μ•ˆμ— λ‹€λ₯Έ λ‚΄μš©μ˜ μ½˜ν…μΈ κ°€ λ“€μ–΄κ°€μžˆλŠ” κ²½μš°μ—λŠ” React children 을 μ‚¬μš©ν•΄λ³΄μž!

μ½”λ“œ μˆ˜μ • μ „

    if (selectedCategory === 'category') {
      return (
        <div className="productCardsGroup">
          <h1>Vitamins</h1>
          {productDatas.map(cardInfo => {
            return <ProductCards cardInfo={cardInfo} />;
          })}

          <h1>Powders</h1>
          {powderDatas.map(cardInfo => {
            return <ProductCards cardInfo={cardInfo} />;
          })}
        </div>
      );
    } else {
      return (
        <div className="productCardsGroup">
          <h1>Eyes</h1>
          {powderDatas.map(cardInfo => {
            return <ProductCards cardInfo={cardInfo} />;
          })}
        </div>
      );
    }
profile
Becoming a front-end developer 🌱

0개의 λŒ“κΈ€