【问题标题】:How can I to display photo in modal?如何以模态显示照片?
【发布时间】:2019-04-17 16:20:05
【问题描述】:

我在发送选择对象照片以模态显示时遇到问题?有人知道我做错了什么吗?我有。

当我点击图片时,我得到一个对象,但它没有在里面放一张图片,我不知道缺少什么?

对于数组我有一个很大的问题我不明白为什么不显示正在发送的对象,我将不胜感激建议和答案。

数组

const dataImage = [
  {
    id: 1,
    name: 'Letniskowy Pałac Branickich',
    img: 'branicki.jpg'
  },
  { 
    id: 2,
    name: 'W drodze do Torunia',
    img: 'roadTorun.jpg'
  },
  {
    id: 3,
    name: 'Kładki w Śliwnie',
    img: 'sliwnoKladki.jpg'
  },
  {
    id: 4,
    name: 'Sopockie Molo',
    img: 'sopotMolo.jpg'
  }
]

我的主要组件

export default function FlexPanelGallery () {
    const [imagesToGallery] = useState(dataImage);
    const [grayscale, setGrayscale] = useState(0);
    const [selectedPhoto, setSelectedPhoto] = useState();
    const [modal, setModal] = useState(false);
    const [subtitle, setSubTitle] = useState('');
    const [modalMainOpen, setModalMainOpen] = useState(false);
    const [pic, setPic] = useState();
    const makeGrey = {
      filter: `grayscale(${grayscale})`
    }

    function toggleModal (pic) {
      setModalMainOpen(true)
      setPic(pic);
      console.log('What is pass! ' + pic)
      return pic
    }
   let photoList = imagesToGallery;
   console.log(photoList);

  return (
    <div>
      <div className="container" style={makeGrey}>        
        {photoList.map((img, i)=> (
          <ImageThumb 
            img={img.img} 
            key={i} 
            name={img.name} 
            onActivePhoto={toggleModal.bind(this, img.img)}
            />
        ))}    
      </div>
      <div style={buttonStyle}>
        <Button variant="contained" color="primary" onClick={() => setGrayscale(4)} >Grayscale</Button>
        <Button variant="contained" color="secondary" onClick={() => setGrayscale(0)} >Normal</Button>
      </div>
{/* When I click this button then i have image in tag <img />  */}
      {/* <button onClick={toggleModal.bind(this, 'http://lorempixel.com/200/200/business/1')} >Click</button> */}


      <button onClick={e => toggleModal(pic)}>Trigger Modal</button>
        <Modal bg="#222" show={ modalMainOpen } 
            onClose={toggleModal.bind(this) }>
            <img src={pic} />
            {pic}
        </Modal>
    </div>
  )
}
class Modal extends React.Component {
  render() {
    const { show, bg, closeModal } = this.props;
    // Custom styles: set visibility and backbround color
    const styles = {
      modal: {
        display: (show) ? null : 'none', 
        backgroundColor: bg || 'rgba(255, 255, 255, 0.8)',       
      }
    };

    return (
      <div className="modal-wrapper" style={styles.modal}>
        <span className="glyphicon glyphicon-remove-sign modal-item"
            onClick={this.props.onClose}></span>
        <div className="modal-item">
            { this.props.children}
                </div>
      </div>
    )
  }
}

我要插入数据的空组件

const ImageThumb = (props) => (
    <div className="cardImage">
      <div className="box">
        <img src={require('./Images/' + props.img)} alt={props.name} onClick={props.onActivePhoto} />
      </div>
      <div className="thumbTitle">{props.name}</div>
    </div> 
);

当我单击图像时,我得到一个对象,但它没有在里面放一张图片我不知道缺少什么?

编辑: 这是这种情况下的结果:https://scherlock90.github.io/flex-panel-gallery/

【问题讨论】:

  • 是否可以通过git共享项目?

标签: arrays reactjs modal-dialog react-hooks


【解决方案1】:

您正在尝试动态包含图像。但是您的图像路径指向./Images/roadTorun.jpg

在浏览器中运行的程序如何识别此路径?

src/FlexComponents/Flex-panel-gallery.js Line 78

<img src={pic} />

如果您想动态定位图像,您需要将您的Images 文件夹移动到output bundle 目录中。在您的情况下,它的 public 目录

对您的项目进行以下更改

  • Images 目录从src/FlexComponents/Images 移动到public/Images

  • Flex-panel-gallery.js 中,使用以下代码更新line 45 中的toggleModel 方法

function toggleModal (pic) {
      setModalMainOpen(true)
      setPic('/Images/'+pic);      
}
  • Flex-panel-gallery.js 中,将line 114 更新为以下代码
<img src={'/Images/' + props.img} alt={props.name} onClick={props.onActivePhoto} />

验证这是否能解决您的问题

注意

line 114 上使用当前代码显示图像没有任何问题

<img src={require('./Images/' + props.img)} alt={props.name} onClick={props.onActivePhoto} />

这是因为你已经静态地包含了图片路径,所以 webpack 在build time 期间导入了图片,这就是为什么图片是在ImageThumb 组件中渲染的

ImageThumb 组件中的以下路径是由 webpack 在 build 时间创建的,因为您在 img 标签中给出了 src 的确切图像路径

【讨论】:

  • 感谢您的帮助!我还有一个问题,如果我想在 src 中拥有所有图像?还有其他构建组件和案例二的途径,还是将图像存储在公共或 src 中更好?因为我知道我正在被静态拍摄,但我不明白为什么我没有拍到相同的照片。
  • 当我想托管结果时,我处于控制台状态 404 并且没有图像加载,但是当我更改函数 setPic(require('./Images/'+pic)); 中的代码时添加点和要求然后一切都很好:P 我很少改变你的建议,但没有你的指导,我不会想到我能做到:-)
  • static import - webpack 在build time 期间知道您将使用一个图像,webpack 将使用file-loader 扫描所有 js 文件中的imported or required 图像并对其进行优化并将其放入正如我在答案中提到的那样,一条不同的路径。如果它是dynamic import,则在build time 期间webpack 不会知道picrequire('/Images/'+pic) 期间build time 将为null。所以,任何放置在src 中的图像都不会被加载,这就是你在modal 中得到空白图像的原因。这就是为什么您必须将 images 从 src 移动到 public 文件夹
  • 好的,但是当我在函数 toggleModal 中设置此代码 -> setPic(require('./Images/'+pic)) 并且我在 src 中有文件夹 Images ;然后一切正常。在 github 主机上它工作正常。
  • 但是如果没有你的帮助,我要感谢你,我不会再继续下去了
猜你喜欢
  • 1970-01-01
  • 2019-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多