8000 Skip setting innerHTML when HTML string has not changed by lukahartwig · Pull Request #32773 · facebook/react · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Skip setting innerHTML when HTML string has not changed #32773

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
Loading
from

Conversation

lukahartwig
Copy link

#26501 introduced a regression when using dangerouslySetInnerHTML which causes innerHTML to be set even when the HTML content did not change.

Resolves #31600, resolves #31660

@facebook-github-bot
Copy link

Hi @lukahartwig!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@sebmarkbage
Copy link
Collaborator

Even if we restore this optimization for perf and profiling purposes (#31660). I'm not sure #31600 is legit. We probably want to make sure to intentionally keep that not working and warn.

That's because createPortal is notoriously under defined in what it should be able to do. If I had it my way it would not accept React owned nodes as containers. It'd have to be manually created ones. It would also only accept one React child per DOM node container. So you couldn't render multiple createPortal into the same target without first creating a new container just like how createRoot clears other content from that container. It's a little late to add these types of restrictions because they're pretty much always used in a way that breaks both these rules. There are many quirks that fall out of that like subtle breakages in the event system and no defined order of which they appear in the DOM. Could change order between versions. It also turns out to create a bunch of problems for View Transitions now too.

Another issue is that React has historically been too forgiving when it comes to where you can insert custom DOM nodes. E.g. you can insert DOM nodes as siblings of React owned nodes using a ref. That's really not supported and will lead to quirks. You can use a ref to insert manual DOM nodes into a child which also has text content, innerHTML or other children. These are all not really supported even if they happen to work sometimes. The only legit way to insert DOM nodes into a ref is if that React element has no other children nor dangerouslySetInnerHTML. I.e. a leaf.

Using a ref as a createPortal container, manually inserting DOM node children into a ref, using the children prop and dangerouslySetInnerHTML are mutually exclusive. They're four different ways to create children that are not compatible with each other.

@sebmarkbage
Copy link
Collaborator

Maybe a warning is enough: #32774

@@ -565,7 +565,10 @@ function setProp(
);
}
const nextHtml: any = value.__html;
if (nextHtml != null) {
if (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can shorten below condition to below

if (nextHtml && prevValue?.__html !== nextHtml) {
...
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to follow the code style here. Optional chaining is used pretty much nowhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
0