-
Notifications
You must be signed in to change notification settings - Fork 49.7k
Implement mutation observer in DOMChildrenOperations #8829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Working on the tests |
| } | ||
|
|
||
| var observer = new MutationObserver(callback); | ||
| var reactRoot = document.querySelector('[data-reactroot]'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
guess this will break if there is multiple react trees on the page
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, i should look for all react trees and run the observer on each of them; will fix.
|
hey @edvinerikson, when you have a chance could you take a look on the questions above? |
|
Are you still working on this @thinkxl? I think it would be helpful to write some tests first and get them approved from the React team. |
|
Modifying nodes inside empty React leaf DOM-nodes is allowed and a feature. This PR does not consider this and warns regardless. Preferably it should also not rely on More info #3218 (comment) |
|
@syranide thanks for the feedback. I will do the changes to meet the requirements. |
You mean when a React component inside a container doesn't have children? <div id="app">
<div data-reactroot></div> <!-- empty react root node -->
</div> <!-- #app -->Or when the tree of React nodes are empty like <div id="app">
<div data-reactroot>
<div class="headerr">
<img src="logo.svg" alt="logo">
</div> <!-- non-empty node -->
<p></p> <!-- empty node, user may want to mutate this externally -->
<div>foo</div> <!-- non-empty node -->
</div>
</div> <!-- #app --> |
|
@thinkxl Yeah, anything inside your "non-empty nodes" is free game and mutations should not emit warnings. @spicyj I'm curious, when it comes to MutationObservers, how do you feel about listeners per React root vs a single global for the document (or per document in the case of iframes)? Having it per React root is a tighter fit but nested React roots will multiply the overhead and nested React roots updating will most likely cause significant overhead due to one or more parent MutationObservers unless you explicitly disconnect parent MutationsObservers but that can also get somewhat expensive. So IMHO a global MutationObserver seems like the simplest and best of these two approaches, but it obviously comes with the downside of being global. The best option would naturally be to have it per React root but not if it's nested, but if you move a nested React root it might not have a MutationObserver covering it anymore (acceptable?). |
|
Looks like some flag is needed on empty leafs which would somehow (using Another option can be not to use deep observation with |
That might be the way to go, having an observer per node, but, not sure about the impact on performance. |
|
As per above comments, this is too strict and will warn for valid cases. I think we might not want to do this after all, as I don't see a way that would be both fast and loose enough. Thanks for trying though! |
This pull request (WIP) tries to resolve #3218
I have done some basic implementation and tried to add a devtool hook, but have some questions:
ReactDebugTool.js, how can I trigger an existing hook to see how it works?.disconnect();function