【问题标题】:React.js - Iterate through card bootstrapReact.js - 遍历卡引导
【发布时间】:2020-06-10 06:31:43
【问题描述】:

我对 React.js 很陌生,如果我有一组图像,连续 3 张卡片,卡片上显示三行,每行之间有空格。 我想知道如何使用以下卡片引导代码遍历图像,

function App() {
    return (

        <Card style={ { width: "18rem" } }>
            <Card.Img variant="top" src="holder.js/100px180" />
            <Card.Body>
                <Card.Title>Card Title</Card.Title>
                <Card.Text>
                    Some quick example text to build on the card
                    title and make up the bulk of
                    the card 's content.
                </Card.Text>
            </Card.Body>
        </Card>
    );
}

【问题讨论】:

    标签: javascript html arrays reactjs react-bootstrap


    【解决方案1】:

    如果您想将多张卡片显示为列表或其他内容,您可以考虑遍历数组并呈现多张卡片,如下所示

    function App() {
      return data.map((i, index) => <CardView key={index} {...i} />);
    }
    
    const CardView = ({
      title = "Default Title",
      text = "Default Text",
      imgsrc = "default_holder.js/100px180"
    }) => (
      <Card style={{ width: "18rem" }}>
        <Card.Img variant="top" src={imgsrc} />
        <Card.Body>
          <Card.Title>{title}</Card.Title>
          <Card.Text>{text}</Card.Text>
        </Card.Body>
      </Card>
    );
    const data = [
      {
        title: "First Title",
        text: "First Text",
        imgsrc: "firstimg.jpg"
      },
      {
        title: "second Title",
        text: "second Text",
        imgsrc: "secondimg.jpg"
      },
      {
        title: "third Title",
        text: "third Text",
        imgsrc: "thirdimg.jpg"
      }
    ];
    
    

    【讨论】:

    • 应该去掉 i: data.map((i, index) =&gt; &lt;CardView key={index} {...i} /&gt;) 周围的括号对
    • 好球!谢谢,更新了!
    猜你喜欢
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多