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
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/react-dom-bindings/src/client/ReactDOMComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.

nextHtml != null &&
(prevValue == null || prevValue.__html !== nextHtml)
) {
if (props.children != null) {
throw new Error(
'Can only set one of `children` or `props.dangerouslySetInnerHTML`.',
Expand Down
36 changes: 36 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,42 @@ describe('ReactDOMComponent', () => {
expect(container.textContent).toEqual('bonjour');
});

it('should set innerHTML when html changed', async () => {
const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<div />);
});
const node = container.firstChild;
const spyOnSetInnerHtml = jest.spyOn(node, 'innerHTML', 'set');
await act(() => {
root.render(<div dangerouslySetInnerHTML={{__html: 'bonjour'}} />);
});
expect(spyOnSetInnerHtml).toHaveBeenCalledTimes(1);
await act(() => {
root.render(<div dangerouslySetInnerHTML={{__html: 'adieu'}} />);
});
expect(spyOnSetInnerHtml).toHaveBeenCalledTimes(2);
});

it('should skip setting innerHTML when html did not change', async () => {
const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<div />);
});
const node = container.firstChild;
const spyOnSetInnerHTML = jest.spyOn(node, 'innerHTML', 'set');
await act(() => {
root.render(<div dangerouslySetInnerHTML={{__html: 'bonjour'}} />);
});
expect(spyOnSetInnerHTML).toHaveBeenCalledTimes(1);
await act(() => {
root.render(<div dangerouslySetInnerHTML={{__html: 'bonjour'}} />);
});
expect(spyOnSetInnerHTML).toHaveBeenCalledTimes(1);
});

it('should not incur unnecessary DOM mutations for attributes', async () => {
const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);
Expand Down