[英]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.