简体   繁体   English

React Uncaught DOMException:无法在“文档”上执行“createElement”:提供的标签名称(“<div> &#39;) 不是有效名称

[英]React Uncaught DOMException: Failed to execute 'createElement' on 'Document': The tag name provided ('<div>') is not a valid name

In react applications, we always have a root component and everything else is a child of that component.在 React 应用程序中,我们总是有一个根组件,其他所有组件都是该组件的子组件。

So what I decided to do is break that convention whenever I am going to display a modal, and create a new element or a new component and append it directly to document.body .所以我决定做的是在我要显示一个模态时打破这个约定,并创建一个新元素或一个新组件并将其直接附加到document.body

a child of body we will not have anymore stacking z-index issues, so this modal will always show up 100% of the time.作为body的孩子,我们将不再有堆叠 z-index 问题,因此该模式将始终显示 100% 的时间。 Or at least that was my thinking.或者至少这是我的想法。

So I made a new component called modal.js所以我创建了一个名为modal.js的新组件

Inside of this modal, rather than returning a div with some fancy css styling on its children I am just going to return a no script tag which means don't render anything like so:在这个模态里面,而不是在它的孩子上返回一个带有一些花哨的 css 样式的 div,我只是要返回一个 no script 标签,这意味着不要渲染任何像这样的东西:

 import React, { Component } from 'react';
    import ReactDOM from ‘react-dom’;

    class Modal extends Component {
       render() {
          return <noscript />;
       }
    }

    export default Modal;

So when I display the modal component its not going to display anything on the screen whatsoever.因此,当我显示模态组件时,它不会在屏幕上显示任何内容。 So then how do I get this modal on the screen then?那么我如何在屏幕上获得这个模式呢?

Well, I decided to do a bit of a workaround by adding in a componentDidMount() like so:好吧,我决定通过添加componentDidMount()来做一些解决方法,如下所示:

import React, { Component } from 'react';
import ReactDOM from ‘react-don’;

class Modal extends Component {
   componentDidMount() {

    }
   render() {
      return <noscript />;
   }
}

export default Modal;

So whenever this component gets mounted or rendered to the screen I am going to create a new div in memory and assign it to this.modalTarget like so:所以每当这个组件被安装或渲染到屏幕上时,我将在内存中创建一个新的 div 并将其分配给this.modalTarget如下所示:

import React, { Component } from 'react'; import React, { Component } from 'react'; import ReactDOM from 'react-don';从“react-don”导入 ReactDOM;

class Modal extends Component {
   componentDidMount() {
      this.modalTarget = document.createElement(‘<div>’);
    }
   render() {
      return <noscript />;
   }
}

export default Modal;

Here is the finished file:这是完成的文件:

import React, { Component } from 'react';
import ReactDOM from 'react-dom';

class Modal extends Component {
  componentDidMount() {
    this.modalTarget = document.createElement('<div>');
    this.modalTarget.className = 'modal';
    document.body.appendChild(this.modalTarget);
    this._render();
  }

  componentWillUpdate() {
    this._render();
  }

  componentWillUnmount() {
    ReactDOM.unmountComponentAtNode(this.modalTarget);
    document.body.removeChild(this.modalTarget);
  }

  _render() {
    ReactDOM.render(<div>{this.props.children}</div>, this.modalTarget);
  }
  render() {
    return <noscript />;
  }
}

export default Modal;

I was expecting for this to work, maybe get an Inviolant error, but certain not:我期待这个工作,也许会得到一个 Inviolant 错误,但肯定不是:

Uncaught DOMException: Failed to execute 'createElement' on 'Document': The tag name provided ('<div>') is not a valid name.未捕获的 DOMException:无法在“文档”上执行“createElement”:提供的标记名称('<div>')不是有效名称。

I am not sure what is going on here.我不确定这里发生了什么。

A comment from Bravo helped me solve this. Bravo 的评论帮助我解决了这个问题。 What helped me was refactoring this:帮助我的是重构这个:

this.modalTarget = document.createElement('<div>');

to this:对此:

this.modalTarget = document.createElement('div');

Why don't you use the React.Fragment ?你为什么不使用React.Fragment

You could do something like that...你可以做这样的事情......

const Modal = () => (
  <React.Fragment>
    <noscript />
  </React.Fragment>
);

export default Modal;

暂无
暂无

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

相关问题 未捕获的 DOMException:无法在“文档”上执行“createElement”:提供的标签名称 - Uncaught DOMException: Failed to execute 'createElement' on 'Document': The tag name provided 无法导入图像:InvalidCharacterError:无法在“文档”上执行“createElement”:提供的标签名称不是有效名称 - Unable to import images: InvalidCharacterError: Failed to execute 'createElement' on 'Document': The tag name provided is not a valid name InvalidCharacterError:无法在“文档”上执行“createElement”:提供的标记名称(“/static/media/tab1.fab25bc3.png”)不是有效名称 - InvalidCharacterError: Failed to execute 'createElement' on 'Document': The tag name provided ('/static/media/tab1.fab25bc3.png') is not a valid name 未捕获的 DOMException:无法在“文档”上执行“querySelector”:“不是有效的选择器” - Uncaught DOMException: Failed to execute 'querySelector' on 'Document': '' is not a valid selector 未捕获的 DOMException:无法在“CustomElementRegistry”上执行“定义”:“[object HTMLElement]”不是有效的自定义元素名称 - Uncaught DOMException: Failed to execute 'define' on 'CustomElementRegistry': “[object HTMLElement]” is not a valid custom element name 错误 DOMException:无法在“元素”上执行“setAttribute”:“{{”不是有效的属性名称 - ERROR DOMException: Failed to execute 'setAttribute' on 'Element': '{{' is not a valid attribute name 由于错误,我的 Vue 应用程序无法安装:未捕获的 DOMException:无法在“元素”上执行“setAttribute”:“)”不是有效的属性名称 - My Vue app won't mount because of an error: Uncaught DOMException: Failed to execute 'setAttribute' on 'Element': ')' is not a valid attribute name 为什么会出现错误提示 Uncaught DOMException: Failed to execute 'querySelector' on 'Document': '#1' is not a valid selector - Why is there an error saying Uncaught DOMException: Failed to execute 'querySelector' on 'Document': '#1' is not a valid selector d3 react js-无法在“文档”上执行“ createElementNS”:提供的限定名称包含无效字符“,” - d3 react js - Failed to execute 'createElementNS' on 'Document': The qualified name provided contains the invalid character ',' 未捕获的 DOMException:无法在反应路由器 dom 6 中对“历史”执行“replaceState” - Uncaught DOMException: Failed to execute 'replaceState' on 'History' in react router dom 6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM