javascript - 将图像数组添加到弹出窗口

标签 javascript reactjs material-ui

我有一个包含体验的组件,每个体验都有一个弹出窗口。我想为每个弹出窗口显示一组图像。

我有不同的图像数组,如 gastronomiaExperience、deportesExperience、bellezaExperience 等。如何将图像数组添加到弹出窗口

这是我的组件

import React, { memo, useCallback } from 'react';
import PropTypes from 'prop-types';
import { makeStyles } from '@material-ui/styles';
import Typography from '@material-ui/core/Typography';
import clsx from 'clsx';
import Popper from '@material-ui/core/Popper';
import gastronomia from 'assets/experiences/gastronomia.jpg';
import productos from 'assets/experiences/productos.jpg';
import giftcard from 'assets/experiences/giftcard.jpg';
import diversion from 'assets/experiences/diversion.jpg';
import deporte from 'assets/experiences/deporte.jpg';
import belleza from 'assets/experiences/belleza.jpg';
import gastronomiaExperiences from 'data/gastronomia';
import productosExperiences from 'data/productos';
import giftcardExperiences from 'data/giftcard';
import diversionExperiences from 'data/diversion';
import deporteExperiences from 'data/deporte';
import bellezaExperiences from 'data/belleza';


// Proptypes definitions to the component.
const propTypes = {
  /** Custom root className. */
  className: PropTypes.string,

};

// Default props definitions.
const defaultProps = {
  className: null,
};

// Component's styles
const useStyles = makeStyles(theme => ({
  root: {
    display: 'block',
    margin: '0 auto',
    maxWidth: '50%',
    [theme.breakpoints.down('md')]: {
      maxWidth: '70%',
    },
    [theme.breakpoints.down('sm')]: {
      maxWidth: '100%',
    },
    '& .experiences-column': {
      display: 'inline-block',
      verticalAlign: 'top',
      textAlign: 'center',
      '&.col1': {
        width: '36.31%',
        [theme.breakpoints.down('sm')]: {
          width: 'initial',
        },
      },
      '&.col2': {
        width: '63.69%',
        [theme.breakpoints.down('sm')]: {
          width: 'initial',
        },
      },
      '& .experience': {
        padding: 2,
        position: 'relative',
        '& img': {
          width: '100%',
          display: 'block',
        },
        '& .experience-title': {
          position: 'absolute',
          bottom: 30,
          left: 0,
          right: 0,
          textAlign: 'center',
        },
      },
    },
  },
}), { name: 'ExperiencesStyle' });



/**
 * Component used to render a grid of experiences.
 *
 * @param {object} props - The component's props.
 * @returns {object} React element.
 */
const Experiences = memo(
  (props) => {
    const { className } = props;
    const classes = useStyles(props);

    const [anchorEl, setAnchorEl] = React.useState(null);


const handleClick = event => {
setAnchorEl(event.currentTarget);
};

  const open = Boolean(anchorEl);

    const experience = (img, title, category, id, popoverCategory) => (
      <div className="experience" aria-describedby={id} id={id} onClick={handleClick} >
        <img
          data-sizes="auto"
          className="lazyload"
          data-src={img}
          alt={title}
        />
        <div className="experience-title">
          <Typography
            color="textSecondary"
            variant="subtitle2"
            className="highlight highlight1"
            display="inline"
          >
            { title }
          </Typography>
        </div>

        <Popper id={id} open={anchorEl && anchorEl.id === id} anchorEl={anchorEl}>
          <div className={classes.paper}>
            <img
          data-sizes="auto"
          className="lazyload"
          data-src={popoverCategory}
          alt="Puntospoint"
        />
          </div>
        </Popper>
      </div>

    );


    return (
      <div className={clsx(classes.root, className)}>
        <div className="experiences-column col1">
          {experience(gastronomia, 'GASTRONOMÍA', 'gastronomia', 'gastronomia', open, anchorEl, gastronomiaExperiences)}
          {experience(giftcard, 'GIFT CARD', 'giftcard','giftcard', open, anchorEl, giftcardExperiences)}
          {experience(deporte, 'DEPORTE', 'deporte', 'deporte', open, anchorEl, deporteExperiences)}
        </div>
        <div className="experiences-column col2">
          {experience(productos, 'PRODUCTOS', 'productos', 'productos', open, anchorEl, productosExperiences)}
          {experience(diversion, 'DIVERSIÓN', 'diversion', 'diversion', open, anchorEl, diversionExperiences)}
          {experience(belleza, 'BELLEZA', 'belleza', 'belleza', open, anchorEl, bellezaExperiences)}
        </div>
      </div>

    );
  },
);

// Component proptypes.
Experiences.propTypes = propTypes;

// Component default props.
Experiences.defaultProps = defaultProps;

export default Experiences;

我是 React 和 Material ui 新手

我尝试使用此 img 标签来引入数组,但它只给了我一张图像

        <img
          data-sizes="auto"
          className="lazyload"
          data-src={popoverCategory}
          alt="Puntospoint"
        />

最佳答案

我以前没有使用过material ui,但如果我假设popoverCategory在url数组中,你的意思是这样的吗?

<Popper>
  <div className={classes.paper}>   
    {
      popoverCatergory.map(url => {
        return <img
          data-sizes="auto"
          className="lazyload"
          data-src={popoverCategory}
          alt="Puntospoint"
        />
      }) 
    }
  </div>
</Popper>

关于javascript - 将图像数组添加到弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59682935/

相关文章:

javascript - 将变量传递到自定义按钮

javascript - 如何在每个 react 组件中设置计时器?

javascript - 强制客户端重新加载我的bundle.js

javascript - 光滑的 slider 用数字替换点

javascript - 如何避免异步javascript中的冗余代码?

javascript - 将状态数据发送到父组件

javascript - 无法绘制 MenuIcon 组件

ios - 如何在 Material-UI 中以与 iOS UITableView 中相同的方式设置 ListSubheader 的样式?

javascript - 旋转捕捉到新的程度,不顺利过渡

javascript - 为什么 "map"不起作用?