【问题标题】:How to add a line break during the process of rendering a component while using a map function in React?在 React 中使用 map 函数时,如何在渲染组件的过程中添加换行符?
【发布时间】:2022-10-04 17:06:26
【问题描述】:

目标:使用map functionadd a line breakevery after the 5th column component is rendered 渲染组件,依此类推,形成以下行。

问题:我不确定如何在映射功能中添加换行符。在添加换行符时,我对操作什么感到困惑。

组件的内容从data.jsx 文件中读取,该文件包含一个对象,该对象具有多个属性,这些属性具有自己的值。

外观问题:

进球出场:

使用地图功能渲染组件的源代码 (Main.jsx):

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


export default function Main(props){
  const { products, onAdd } = props;
    return (
      <main className="block col-2">
        <h2>Products</h2>
        <div className="row">
          {products.map((product) => (
            <Product key={product.id} product={product} onAdd={onAdd} />
          ))}
        </div>
      </main>
    );
}

data.jsx的源代码:

const data = {
  products: [
    {
      id: "1",
      name: "MacBook",
      price: 1400,
      image: "https://picsum.photos/id/180/2400/1600"
    },
    {
      id: "2",
      name: "Old Car",
      price: 2400,
      image: "https://picsum.photos/id/111/4400/2656"
    },
    {
      id: "3",
      name: "W Shoes",
      price: 1000,
      image: "https://picsum.photos/id/21/3008/2008"
    },

    {
      id: "4",
      name: "W Shoes 2",
      price: 1200,
      image: "https://picsum.photos/id/21/3008/2008"
    },

    {
      id: "5",
      name: "W Shoes 3",
      price: 1300,
      image: "https://picsum.photos/id/21/3008/2008"
    },

    {
      id: "6",
      name: "W Shoes 3",
      price: 1300,
      image: "https://picsum.photos/id/21/3008/2008"
    },

    {
      id: "7",
      name: "W Shoes 3",
      price: 1300,
      image: "https://picsum.photos/id/21/3008/2008"
    },

    {
      id: "8",
      name: "W Shoes 3",
      price: 1300,
      image: "https://picsum.photos/id/21/3008/2008"
    }
  ]
};
export default data;

CodeSandbox 中功能齐全的应用程序:

https://codesandbox.io/s/renderbuttonsnewlinefixing-cherry-h0br4o-forked-88d2q7?file=/src/components/Main.jsx

类似堆栈问题:how do i generate a line break inside a loop in react?

(但我不确定如何从这个问题中准确实现这种方式,因为它对我来说效果不佳)

您的回答确实对我有很大帮助,因为我目前正在学习 React 基础知识,涉及映射和渲染组件。非常感谢!

【问题讨论】:

    标签: reactjs react-native loops mapping rendering


    【解决方案1】:

    试试这个请

      {products.length && products.map((product, index) => 
            <>
                  <Product key={product.id} product={product} onAdd={onAdd} />
                  {products.length > index && <br/>}
            </>
       )}
    

    【讨论】:

    • 您好@HacıCelalAygar,目前它仍然无法正常工作,因为它仅在一行上连续呈现组件,并且仍然不会为每第 5 列组件发出换行符。我不确定第 5 列组件将给出换行符的条件在哪里。
    • 我编辑代码,你能试试这个代码吗
    • 不,不起作用,它仍然连续呈现在一行上并破坏了页面的整体外观。
    • 希望您对此有另一个很好的回应。
    【解决方案2】:

    如果您使用引导程序,您可以利用 flexbox 样式并利用 flex-wrap 属性,然后调整您的 max-width 直到您可以看到每行仅呈现 5 个项目。

    <div class="d-flex flex-wrap" style={{ maxWidth: YOUR_MAX_WIDTH_HERE }}>
      {products.map((product) => (
            <Product key={product.id} product={product} onAdd={onAdd} />
       ))}
    </div>
    

    【讨论】:

    • 哦,很好,这是一个很好的反应依赖 CSS 框架,但很好的建议。但是当我将它应用到 Main.jsx 中的代码时,它以一种奇怪的方式呈现,它会在每个呈现的组件之后自动添加一个换行符。更新的代码框:codesandbox.io/s/…(很高兴在 Stack 见到一位 kababayan 菲律宾 React 开发人员^_^)
    • 希望您对此有另一个很好的回应。
    【解决方案3】:

    如果你选择 React Bootstrap 或 AntD,你可以使用网格系统快速实现这一点。 https://ant.design/components/grid/

    但是,就您的要求而言,这应该可行:)。用https://playcode.io/react 测试了它

    const displayProducts = (products)=>{
    return products.map((product, i)=>{
      return (
        <div key={i}>
          <Product/>
          { (i+1) % 5 === 0 ? <br/> : null}
        </div>)
    });
    

    }

    【讨论】:

    • 很好的条件方法,但我认为你快到了。只是这里的渲染是垂直显示而不是水平显示。
    • 我更新了我的问题图片,以便您可以清楚地看到目标外观@iWiiCK
    【解决方案4】:

    这对你有用吗?我假设您只需要换行符,而不是文字 &lt;br /&gt;

    import React from "react";
    import Product from "./Product";
    
    export default function Main(props) {
      const { products, onAdd } = props;
    
      return (
        <main className="block col-2">
          <h2>Products</h2>
          <div
            style={{
              display: "grid",
              gridTemplateColumns: "repeat(5, 1fr)"
            }}
          >
            {products.length &&
              products.map((product) => (
                <Product key={product.id} product={product} onAdd={onAdd} />
              ))}
          </div>
        </main>
      );
    }
    

    gridTemplateColumns: "repeat(5, 1fr)" 这一行中的 1fr 更改为您想要的每个产品的任何宽度。 1fr 表示空间为每个产品平分。

    【讨论】:

      猜你喜欢
      • 2020-10-10
      • 2018-10-01
      • 2018-12-24
      • 1970-01-01
      • 2021-04-19
      • 2020-04-03
      • 2016-03-06
      • 1970-01-01
      相关资源
      最近更新 更多