简体   繁体   English

如何在react js中重置img标签?

[英]How to reset img tag in react js?

Each row has different image source under the each image icon.每行在每个图像图标下都有不同的图像源。 But the problem is that after clicking on the image icon the images popup happens and maybe it is stored in the cache and when I click the other image icon I'm getting the same image as popup.但问题是,在单击图像图标后,图像弹出窗口可能存储在缓存中,当我单击另一个图像图标时,我得到的图像与弹出窗口相同。 So, how can I reset the image tag?那么,如何重置图像标签? or what should I do?或者我该怎么办? The code is given below.代码如下。

在此处输入图片说明

QueueManager.js:队列管理器.js:

const [items1, setItems1] = useState({
toggle: false,
data_form: [],
cardguid: '',
buttonDisable: "",
isOpen: false,
});

const handleShowDialog = () => {
setItems1({...items1,isOpen: !items1.isOpen,});
// console.log("cliked");
// alert('Yes');
};

Table.js:表.js:

  <div>
    <span onClick={(e) => props.handleShowDialog()} style={{marginLeft: '30%'}}><i class='fas fa-image'></i></span>

    {props.isOpen && (
      <dialog
        className="dialog"
        style={{ position: "absolute" }}
        open
        onClick={(e) => props.handleShowDialog()}
      >
        <img
          className="photo"
          src= {'...' + props.item.cardguid.replace(/-/g, '') + '_front.jpg'}
          alt="no image"
        />
        <img
          className="photo"
          src= {'...' + props.item.cardguid.replace(/-/g, '') + '_back.jpg'}
          alt="no image"
        />
      </dialog>
    )} 
  </div>

I have updated the same for you, There were couple of bugs:我已经为您更新了相同的内容,有几个错误:

  1. You were using same variable to control both the images您使用相同的变量来控制两个图像
  2. You had accidentally put the src of image in second div same as that of first one.您不小心将图像的 src 放在与第一个相同的第二个 div 中。

Here is the updated sandbox这是更新的沙箱

https://codesandbox.io/s/how-to-set-a-modal-popup-with-an-image-in-react-forked-e5f50?file=/src/component/ImageComponent.js:0-1793 https://codesandbox.io/s/how-to-set-a-modal-popup-with-an-image-in-react-forked-e5f50?file=/src/component/ImageComponent.js:0-1793

I have used two separate handlers for image, just so that you get the point.我为图像使用了两个单独的处理程序,只是为了让您明白这一点。

If the length is not known you need to run a map function and in handleClick you can pass the index to change the size如果长度未知,您需要运行地图功能,并且在 handleClick 中您可以传递索引以更改大小

import React from "react";
import "../styles.css";
export default class ImageComponent extends React.Component {
  state = { isOpen: false };
  state = { isOpen2: false };

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

  render() {
    return (
      <div>
        <img
          className="small"
          src="https://kronozio.blob.core.windows.net/images/card/c5d61f9b124648b2ae4cbab63011fbc4_front.jpg"
          height={150}
          weight={200}
          onClick={this.handleShowDialog}
          alt="no image"
        />
        {this.state.isOpen && (
          <dialog
            className="dialog"
            style={{ position: "absolute" }}
            open
            onClick={this.handleShowDialog}
          >
            <img
              className="image"
              src="https://kronozio.blob.core.windows.net/images/card/c5d61f9b124648b2ae4cbab63011fbc4_front.jpg"
              onClick={this.handleShowDialog}
              height={150}
              weight={200}
            />
          </dialog>
        )}

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

I've already done mapping over the image.我已经完成了对图像的映射。 Here's the code:这是代码:

  // mapping our fetched data to table.here cardguid is our unique key to 
  distinguish all our data
  const data1 = items1.elements.map((item) => (
<Table_QueueManager
  key={item.cardguid}
  item={item}
  handleShowDialog={handleShowDialog}
  onRadioChange={onRadioChange}
  // clickImage = {clickImage}
  isOpen={items1.isOpen}
  img_url={items1.img_url}
/>
 ));

This code is for the client end, where I put the image dynamically此代码用于客户端,我在其中动态放置图像

     <td>
      <div>
    <span onClick={(e) => props.handleShowDialog()} style={{marginLeft: '30%'}}><i class='fas fa-image'></i></span>
    
    
    {props.isOpen && (
      <dialog
        className="dialog"
        style={{ position: "absolute" }}
        open
        onClick={(e) => props.handleShowDialog()}
      >
        <img
          className="photo"
          src= {'https://kronozio.blob.core.windows.net/images/card/' + props.item.cardguid.replace(/-/g, '') + '_front.jpg'}
          alt="no image"
        />
        <img
          className="photo"
          src= {'https://kronozio.blob.core.windows.net/images/card/' + props.item.cardguid.replace(/-/g, '') + '_back.jpg'}
          alt="no image"
        />
      </dialog>
    )} 
  
  </div>
      

      </td>

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

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