简体   繁体   English

如何在 React JS 中从数组中渲染多个图像?

[英]How to render multiple images from array in React JS?

everyone.每个人。 I'm fairly new to React Js, and I got stuck while trying to render images from an array.我对 React Js 相当陌生,在尝试从数组中渲染图像时遇到了困难。 My code is:我的代码是:

import { home } from "../homeObj";


const Logos = ({ title, img, img2, img3, key }) => {
    return (
        <>
            <div className={styles.anaShif} key={key}>
                <h2 className={styles.h2}> {title} </h2>
                <div className={styles.logos}>
                    <img
                        id={img.id}
                        src={img}
                        alt={img.alt}
                        className={styles.img}
                        srcSet={`${img2} 2x, 
                        ${img3} 3x`}
                    />
                </div>
            </div>
        </>
    );
};

function Trusted() {
    const logoComponent = home.map((_objects, i, id) => {
        if (home[i].id === "logos") {
            return (
                <>
                    <Logos
                        key={i}
                        id={id}
                        title={home[i].title}
                        img={home[i].logos[i].src}
                    />
                </>
            );
        }
    });

    return (
        <>{logoComponent}</>

        );
    }

    export default Trusted;

and { home }:和{家}:

export const home = [

{
        id: "logos",
        title: "Welcome to",
        logos: [
            {
                id: 1,
                alt: "car",
                src: require("./logo_06.png"),
                src2: require("./logo_06@2x.png"),
                src3: require("./logo_06@2x.png"),
            },
            {
                id: 2,
                alt: "red-car",
                src: require("./logo_07.png"),
                src2: require("./logo_07@2x.png"),
                src3: require("./Tlogo_07@3x.png"),
            },
        ],
]

What happens is that I can only display an image of the 2nd element of logos array, it's like React Js completely skips 1st element(id, img.src, alt).发生的情况是我只能显示 logos 数组的第二个元素的图像,就像 React Js 完全跳过了第一个元素(id,img.src,alt)。

What I want to do is to be able to display both images at the same time and also add elements dynamically, when a new element gets added to {home}, it should display without hardcoding.我想要做的是能够同时显示两个图像并动态添加元素,当一个新元素被添加到 {home} 时,它应该在没有硬编码的情况下显示。

Any help would be greatly appreciated.任何帮助将不胜感激。

You have to return the map of logos which is not happening in your current code.您必须返回当前代码中未发生的徽标的 map。 Hence you are getting only one Logos component.因此,您只得到一个 Logos 组件。

import React from 'react';
import { home } from '../homeObj';

const Logos = ({ title, img, img2, img3, key }) => {
  return (
    <>
      <div className={styles.anaShif} key={key}>
        <h2 className={styles.h2}> {title} </h2>
        <div className={styles.logos}>
          <img
            id={img.id}
            src={img}
            alt={img.alt}
            className={styles.img}
            srcSet={`${img2} 2x, 
                        ${img3} 3x`}
          />
        </div>
      </div>
    </>
  );
};

function Trusted() {
  const logosIndex = home.findIndex((obj) => obj.id === 'logos');
  const logos = home[logosIndex].logos.map(({ id, alt, src }) => {
    return <Logos key={id} id={id} title={home[logosIndex].title} img={src} />;
  });

  return logos;
}

export default Trusted;

You not only need to map over home but also the logos array with-in each home object.您不仅需要 map 超过home ,还需要每个home object 中的logos数组。 Also in your case, you don't need the third arg which is the entire array.同样在您的情况下,您不需要作为整个数组的第三个参数。

Simplify your code like this:像这样简化您的代码:

home.map((_objects, i) => {
    if (_objects.id === "logos") {
        return (
            <>
                {
                    _objects.logos.map(logo => ( 
                        <Logos
                            key={logo.id}
                            id={logo.id}
                            title={logo.title}
                            img={logo.src}
                        />
                    ))
                }
            </>
        );
    }else {
        return null
    }
}

Without much if else, you can also write like this.不用太多if else,你也可以这样写。

 home.filter((_objects, i) => _objects.id === 'logos').map(({logos})=>logos.map(logo=> <Logos key={logo.id} {...logo} />))

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

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