[英]Unexpected behavior when DOM structure is modified and re-rendered in react
I have the following code which is built using react. 我有以下代码是使用react构建的。
<div id="wrapper">
<form onSubmit={this.handleSubmit}>
<div id="content">
.....
</div>
</form>
</div>
Here, a third party library updates DIV#content on regular basis. 在这里,第三方库会定期更新DIV#content 。 However, On submission of the form, I get to wrap the DIV#content with another DIV from my code.
但是,提交表单后,我将用我的代码中的另一个DIV包装了DIV#content 。 Please find the it below after the modification on submission:
提交修改后,请在下面找到它:
<div id="wrapper">
<form onSubmit={this.handleSubmit}>
<div id="contentWrapper">
<div id="content">
.....
</div>
</div>
</form>
</div>
But as soon as the DIV#content is wrapped by another DIV, third party library stops working. 但是,一旦DIV#内容被另一个DIV包装,第三方库就停止工作。 Can anyone tell me why it stops updating the DIV#content once the DOM structure is modified despite the fact that the DIV#content element is still available to library.
谁能告诉我为什么修改了DOM结构后,它会停止更新DIV#content ,尽管DIV#content元素仍可用于库。
Note: React is not throwing any error about the DOM structure is changed but just the library stops working. 注意:React不会引发有关DOM结构已更改的任何错误,而只是库停止工作。
[Edit]: Here is the way I am rendering using react [编辑]:这是我使用react渲染的方式
render() {
var returnIt;
if (useDivOne) returnIt = (<div id='initial'></div>);
else returnIt = (<div id='modified'></div>);
return (returnItj);
}
When React is rendering it's not re-using the old DOM element. 当React渲染时,它不会重新使用旧的DOM元素。 So your library still has a reference to the old DOM reference and not the new one that react is recreating when it re-renders.
因此,您的库仍然具有对旧DOM引用的引用,而不是在重新渲染时重新创建的新的DOM引用。 Since your library isn't primed for react, you could go about doing this in a sort of round about way, by guaranteeing that the dom element you pass to your library never gets re-created:
由于您的库没有准备好进行反应,因此可以通过确保不会再创建传递给库的dom元素来以某种方式进行处理:
componentDidUpdate() {
// this makes sure yourDiv is ALWAYS visible inside your
// component
if(!this.yourDiv.parentNode) {
this.refs.libContainer.appendChild(this.yourDiv);
}
}
render() {
// you will pass a reference to yourDiv to your library and it will
// refer to that. YourDiv is NEVER re-created so this DOM element
// lives forever and your library remains happy
if(!this.yourDiv) {
this.yourDiv = document.createElement('div');
this.yourDiv.className = 'specialDiv';
this.yourDiv.innerHTML = 'hahahaa';
}
return <div ref="libContainer"></div>;
// .... or sometimes return this
return <div className="wrapper"><div ref="libContainer"></div></div>;
}
And you would pass your library a reference to .specialDiv
- note that we're completely stepping over how react renders the DOM here and that this approach is only really viable when fixing a problem where react is interfering with what you're trying to accomplish - so if you implement this I suggest making a new react component as a wrapper around your library so that all the "ugliness" to make the library react friendly can be contained in 1 component. 您将为您的库传递对
.specialDiv
的引用-请注意,我们已经完全解决了react如何在此处呈现DOM,并且这种方法仅在解决react干扰您要完成的问题时才真正可行。 -因此,如果实现此功能,我建议制作一个新的react组件作为库的包装,以便使库能够友好响应的所有“丑陋”都可以包含在一个组件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.