简体   繁体   English

如何在 React 中使用图像设置模态弹出窗口

[英]How to set a Modal popup with an image in React

I have an Image class in React, and I have a button that should implement expand on click, on every image.我在 React 中有一个 Image 类,我有一个按钮,应该在每个图像上实现单击时展开。 I decided to use a Modal popup so when I click, the Image will show bigger in the Modal popup.我决定使用模态弹出窗口,所以当我点击时,图像会在模态弹出窗口中显示得更大。 I find it hard to set the Modal as an image.我发现很难将 Modal 设置为图像。

Thank you in advance.先感谢您。

This is from the Image class in React:这是来自 React 中的Image类:

<FontAwesome
  className="image-icon"
  name="expand"
  title="expand"
  onClick={this.showModal}
/>
<Modal show={this.state.isExpand} handleClose={this.hideModal} />

Modal:模态:

const Modal = ({ handleClose, show }) => {
  const showHideClassName = show ? 'modal display-block' : 'modal display-none';
  return (
    <div className={showHideClassName}>
      <section className="modal-main">
        <button onClick={handleClose}>close</button>
      </section>
    </div>
  );
};

Try this sample code.试试这个示例代码。 Here is a link to the example ImageComponent https://codesandbox.io/s/4xnxqz0ylx这是示例 ImageComponent https://codesandbox.io/s/4xnxqz0ylx的链接

export default class ImageComponent extends React.Component {
  state = { isOpen: false };

  handleShowDialog = () => {
    this.setState({ isOpen: !this.state.isOpen });
    console.log('cliked');
  };

  render() {
    return (
      <div>
        <img
          className="small"
          src="/Anj.png"
          onClick={this.handleShowDialog}
          alt="no image"
        />
        {this.state.isOpen && (
          <dialog
            className="dialog"
            style={{ position: 'absolute' }}
            open
            onClick={this.handleShowDialog}
          >
            <img
              className="image"
              src="/Anj.png"
              onClick={this.handleShowDialog}
              alt="no image"
            />
          </dialog>
        )}
      </div>
    );
  }
}

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

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