简体   繁体   English

为什么 document.getElementById() 在 React 中返回 null 尽管 React 中有元素?

[英]Why is document.getElementById() returning null in React although element is there in React?

I have this code inside a React class component and when I try calling document.getElementById(photo._id) it keeps returning null although when I tried console.log(photo._id) in both lines, the line in the render is printed first in the console so the id is supposed to be there?我在 React class 组件中有这段代码,当我尝试调用document.getElementById(photo._id)它一直返回 null 虽然当我在两行中尝试console.log(photo._id)时,首先打印渲染中的行在控制台中所以id应该在那里?

componentDidMount() {
    this.props.location.state.product.photos.map((photo) => {
      return axios
        .get(`${SERVER_HOST}/products/photo/${photo.filename}`)
        .then((res) => {
          if (res.data) {
            if (res.data.errorMessage) {
              console.log(res.data.errorMessage);
            } else {
              console.log(photo._id);
              console.log(document.getElementById(photo._id));
            }
          } else {
            console.log("Record not found");
          }
        });
    });
  }

render() {
    return (
    {this.props.location.state.product.photos.map((photo) => {
         { console.log(photo._id); }
         <img key={photo._id} id={photo._id} />;
   
     })}
  )
}

Here is a picture of the console这是控制台的图片

You are not returning the image element in map function. Hence, it's not mounted in the dom tree and you can't access it using document.getElementById()您没有返回 map function 中的图像元素。因此,它没有安装在 dom 树中,您无法使用 document.getElementById() 访问它

render() {
    return (
    {this.props.location.state.product.photos.map((photo) => {
         { console.log(photo._id); }
          {/** return keyword is missing here **/}
         return <img key={photo._id} id={photo._id} />;
   
     })}
  )
}

Replace your render function body with the code below.用下面的代码替换你的渲染 function 主体。

return this.props?.location?.state?.product?.photos?.map((photo) => 
   <img key={photo._id} id={photo._id} src={...} />;

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

相关问题 当存在具有该 ID 的元素时,为什么 document.getElementById() 返回 null? - Why is document.getElementById() returning null when there is an element with that Id? React(16.8.3) document.getElementById() 在 componentDidMount 中返回 null - React(16.8.3) document.getElementById() returning null in componentDidMount 为什么document.getElementById返回空值? - Why is document.getElementById returning a null value? document.getElementById返回null - document.getElementById is returning null 无法使用document.getElementById获取元素,返回null - not able to get an element using the document.getElementById, returning null 对于已知元素,document.getElementById返回null - 为什么? - document.getElementById coming back with null for a known element - why? document.getElementById在单击按钮时返回null - document.getElementById is returning null on the button click CKEditor主体上的Document.getElementById返回null - Document.getElementById on CKEditor body returning null document.getElementById 每次都返回 null 或 undefined - document.getElementById is returning null or undefined everytime 返回TypeError:document.getElementById(…)为null - Returning TypeError: document.getElementById(…) is null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM