简体   繁体   English

动态切片产品数据数组以显示在 ReactJs 组件中

[英]dynamically slice array of product data to display in ReactJs component

I'm building an ecommerce store with Reactjs where I need to display the products in home component dynamically.我正在使用 Reactjs 构建电子商务商店,我需要在其中动态显示家庭组件中的产品。 Now in the home component the products should follow some styling like for example 4 products in first row, 1 in second row, 3 in the next row etc. I'm calling all the product data array from the database as products and passing them in another component called Products component by mapping through the products array.现在在 home 组件中,产品应该遵循一些样式,例如第一行 4 个产品,第二行 1 个,下一行 3 个等。我将数据库中的所有产品数据数组称为产品并将它们传入另一个组件称为Products component ,通过产品数组映射。 But in doing so I'm using slice() method and hard coding how many and which products should pass in the in each rows.但是在这样做时,我使用slice()方法并硬编码每行中应该传递多少和哪些产品。 Is there a way instead of hardcoding number of products per row like Array.slice(0,4) I can achieve the same styling in a more dynamic way有没有办法而不是像Array.slice(0,4)这样对每行的产品数量进行硬编码,我可以以更动态的方式实现相同的样式

import React, { useState, useEffect } from "react";
import "./Home.css";
import Product from "./Product";
import { db } from "./firebase";

function Home() {
  const [products, setProducts] = useState([]);

  useEffect(() => {
    db.collection("products").onSnapshot((snapshot) => {
      setProducts(
        snapshot.docs.map((doc) => ({ id: doc.id, products: doc.data() }))
      );
    });

return (
    <div className="home">
        <div className="home__container">

          <div className="home__row">
                 {products.slice(0, 4).map(({ id, products }) => (      //Hard coding number of products
            <Product 
              key={id}
              title={products.title}
              price={products.price}
              image={products.image}
              rating={products.rating}
            />
          ))}
        </div>

          <div className="home__row">

              {products.slice(4).map(({ id, products }) => (            //Hard coding number of products
            <Product
              key={id}
              title={products.title}
              price={products.price}
              image={products.image}
              rating={products.rating}
            />
          ))}
        </div>

          <div className="home__row">
                {products.slice(5, 10).map(({ id, products }) => (      //Hard coding number of products
            <Product
              key={id}
              title={products.title}
              price={products.price}
              image={products.image}
              rating={products.rating}
            />
          ))}
        </div>

Actually you can achieve the same thing with CSS.实际上,您可以使用 CSS 实现相同的目的。 Set display: flex for parent div and flex: 0 25% for each product item:设置 display: flex for parent div 和 flex: 0 25% for each product item:

<div style={{style: "display: flex"}}>
  {

    products.map((product, index)=>{
      <div style={{flex: "0 25%"}}>
        <Product 
              key={id}
              title={product.title}
              price={product.price}
              image={product.image}
              rating={product.rating}
            />
      </div>  
    })
  }
</div>

But in case you still prefer your current approach then I can suggest this solution:但是,如果您仍然更喜欢当前的方法,那么我可以建议以下解决方案:

{
  const productWithRows = products.reduce((memo, product, index)=>{
    memo[Math.floor(index/4] = product;
    return memo;
  },[])

  productWithRows.map((productsPerRow, index)=> {
    <div className="home__row">   
      {productsPerRow.map((product)=>{

        <Product 
          key={id}
          title={product.title}
          price={product.price}
          image={product.image}
          rating={product.rating}
        />
      })}
  </div>
 })
}
follow some styling like for example 4 products in first row, 1 in second row, 3 in the next row etc.

这使您进行硬编码,如果您想避免这种情况,您应该通过道具传递这些数字(规则)或从数据库中获取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM