简体   繁体   English

ReactJS:更新组件时无法在“节点”上执行“ removeChild”

[英]ReactJS: Failed to execute 'removeChild' on 'Node' when updating component

I keep getting this error when attempting to update a component with new data fetched from a server. 尝试使用从服务器获取的新数据更新组件时,我一直收到此错误。

Description: 描述:

  • Portfolio.js Portfolio.js

    I have an app that has a portfolio where the projects of the site are shown. 我有一个应用程序,其中包含显示站点项目的项目组合。 When you click an item in portfolio, you are redirected (with react-router) to the individual project. 单击投资组合中的项目时,您将被重定向(使用react-router)到单个项目。

  • Project.js Project.js

    The project component uses the fetch api to retrieve data from a server. 项目组件使用访存api从服务器检索数据。 Which works perfectly because when the url specifies the project to be show like: /portfolio/project/:id , and based on the id received the data is fetched and displayed correctly. 之所以能完美运行,是因为当url指定要显示的项目时,例如: /portfolio/project/:id ,并根据收到的ID正确提取并显示数据。

  • The data retrieved 检索到的数据

    What is retrieved trough fetch api is just the result of a AJAX POST request to a server with the only parameter of the project id, which only returns the info of the project. 通过获取api检索的内容只是对具有项目ID唯一参数的服务器的AJAX POST请求的结果,该参数仅返回项目的信息。 (name, description, images, and other ones not much important) (名称,说明,图像等不太重要)

The issue: 问题:

As far I've analyzed the application to see when this error is triggered, I found that this warning comes out when the amount of images for the new project loaded is smaller than the amount of images of the project already rendered. 到目前为止,我已经分析了应用程序以查看何时触发了此错误,我发现当为新项目加载的图像数量小于已渲染的项目的图像数量时,会发出此警告。

An example: 一个例子:

We are inside a project. 我们在一个项目中。 The url is portfolio/project/1 and the server data returned that this project has 5 images. 网址为portfolio/project/1 ,返回的服务器数据表明该项目有5张图片。 They are loaded and you can view the project correctly. 它们已加载,您可以正确查看项目。

Now, we use the menu to react-router-redirect to portfolio/project/2 and the server returned data with 7 images. 现在,我们使用菜单将router-redirect重定向到portfolio/project/2 ,服务器返回包含7个图像的数据。 They are loaded and the project data as well to view the project. 它们已加载,并且项目数据也可以查看项目。

So, let say that we choose the option in the menu to view the project portfolio/project/3 which has 3 images, and then the warning comes up. 因此,假设我们选择菜单中的选项以查看包含3张图像的项目portfolio/project/3 ,然后出现警告。

Browser: 浏览器: 错误内容 Console log: 控制台日志: 控制台日志

The code: 编码:

Since the error says the problem is inside Project.js, I am only adding this code so the question doesn't look overloaded and full of weird code. 由于错误表明问题出在Project.js内部,因此我仅添加此代码,因此该问题看上去不会过载并且充满奇怪的代码。

Project.js Project.js

import React,{Component} from 'react'
import Footer from '../Footer/Footer'
import Header from '../Header'
import SubNav from '../SubNav'
import Details from './Details'
import {Link} from 'react-router-dom'
import {Configurations} from '../AppConfig'

class Project extends Component{

state = {
            projectInfo: null,
            reloadHandlerActive: false,
            projectID : this.props.match.params.id,
            projectName: "",
            topProjectInfo: [],
            images: []
}

createImages = (project) =>{
  let Images = Object.values(project.images),
      ImagesURI = Images.map((img, index)=>{
        if( img.includes('Desarrollos.jpg') || img.includes('Home.jpg') || img.includes('H.jpg') ){
          return null
        }
      return project.path+img
      })
  ImagesURI = ImagesURI.filter(function (e) { //Clear null values
    return e != null;
  })
  return ImagesURI
}

    reloadHandler = (id) =>{
      const {createImages} = this
      fetch(Configurations.API.projectInfo(id))
        .then((result)=>{return result.json() })
        .then((project)=>{
          if(project === "error"){
            alert("Error")
          }else{
              this.setState({
                projectInfo: project,
                images: createImages(project)
              },function(){
                document.getElementsByClassName("nav-button")[0].click()
              })
          }
        })
    }

    componentWillMount(){
      const {createImages} = this
      fetch(Configurations.API.projectInfo(this.state.projectID))
        .then((result)=>{return result.json() })
        .then((project)=>{
          if(project === "error"){
            alert("Error")
          }else{
            this.setState({
              projectInfo: project,
              images: createImages(project)
            },function(){
              window.initDogma()
            })
          }
        })
    }

    componentDidMount(){
      window.onload = window.initShit()
    }

    render(){
      const {projectInfo,images} = this.state
      console.log(projectInfo)

      if(!projectInfo){
        return(<h1>. . .</h1>)
      }
      return(
          <div >
              <Header />
              <SubNav reloadHandler={this.reloadHandler} />
              <div className="content full-height no-padding">

              <div className="fixed-info-container">
              <Link to="/portfolio"><button className="goBackBtn">Desarrollos</button></Link>
                <h3>{projectInfo.project.project}</h3>
                  <div className="separator" />
                  <div className="clearfix" />
                  <p>
                    {projectInfo.project.address}
                  </p>
                  <span className="project-status">{projectInfo.project.status}</span>
                  <h4>Características</h4>
                  <Details price={projectInfo.project.price} features={projectInfo.project.features} />
                  <Link className=" btn anim-button   trans-btn   transition  fl-l" to={"/contact/?project="+projectInfo.id}> 
                    <span>Informes</span>
                    <i className="fa fa-eye" />
                  </Link>
                  </div>


              {/*  fixed-info-container end*/}
              {/*  resize-carousel-holder*/}
              <div className="resize-carousel-holder vis-info gallery-horizontal-holder">
                {/*  gallery_horizontal*/}
                <div
                  id="gallery_horizontal"
                  className="gallery_horizontal owl_carousel"
                >
                  {
                    images.map((img,index)=>{
                      return (
                      <div key={index}className="horizontal_item">
                        <div className="zoomimage">
                          <img src={img} className="intense" alt="" />
                          <i className="fa fa-expand" />
                        </div>
                        <img src={img} alt="" />
                        <div className="show-info">
                          <span>Info</span>
                          <div className="tooltip-info">
                            <h5>Imagen de muestra</h5>
                            <p>
                              Imagen del desarrollo
                            </p>
                          </div>
                        </div>
                      </div>
                      )
                    })
                  }
                </div>
                {/*  resize-carousel-holder*/}
                {/*  navigation */}
                <div className="customNavigation">
                  <a href="/" className="prev-slide transition">
                    <i className="fa fa-angle-left" />
                  </a>
                  <a href="/" className="next-slide transition">
                    <i className="fa fa-angle-right" />
                  </a>
                </div>
                {/*  navigation end*/}
              </div>
              {/*  gallery_horizontal end*/}
            </div>
            <Footer />
            </div>
        )
    }
}
export default Project

I'm currently looking how to fix this, but if someone could give me an advice or the origin of the issue would be really helpful. 我目前正在寻找解决方法,但如果有人可以给我建议或问题的根源将非常有帮助。

By the time your question was posted, there was an issue open at reactjs github repo talking about something similar. 在发布您的问题时,reactjs github存储库中存在一个谈论类似问题的问题。 It is now solved, but you may want to read the article . 现在已解决,但您可能需要阅读该文章

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

相关问题 DOMException: 无法在“节点”上执行“removeChild”。 使用地图渲染组件时 - DOMException: Failed to execute 'removeChild' on 'Node'. When render with a map a component 无法在“节点”上执行“removeChild” - Failed to execute 'removeChild' on 'Node' 无法在节点上执行 removeChild - Failed to execute removeChild on Node React - 无法在“节点”上执行“removeChild” - React - Failed to execute 'removeChild' on 'Node' 无法在“节点”上执行“removeChild”:参数 1 的类型不是“节点” - Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node' NotFoundError:无法在 React with Flickity 上的“节点”上执行“removeChild” - NotFoundError: Failed to execute 'removeChild' on 'Node' on React with Flickity 无法使用&amp;&amp;逻辑运算符在“节点”上执行“ removeChild” - Failed to execute 'removeChild' on 'Node' with && logical operator 如何解决无法在“节点”上执行“ removeChild”? - How to resolve Failed to execute 'removeChild' on 'Node'? 无法在“节点”滑出错误上执行“ removeChild” - Failed to execute 'removeChild' on 'Node' slideout error 无法在“节点”上执行“ removeChild”:要删除的节点不是该节点的子节点 - Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM