Skip to content

Commit

Permalink
avoid removing existing dom nodes on subsequent replaceNode calls
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Jan 22, 2020
1 parent a311b04 commit 3a54222
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ function diffElementNodes(
}
} else if (newVNode !== oldVNode) {
if (excessDomChildren != null) {
excessDomChildren[excessDomChildren.indexOf(dom)] = null;
excessDomChildren = EMPTY_ARR.slice.call(dom.childNodes);
}

Expand Down
23 changes: 23 additions & 0 deletions test/browser/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,29 @@ describe('render()', () => {
options._diff = prevDiff;
});

describe('subsequent replaces', () => {
it("shouldn't remove elements", () => {
const placeholder = document.createElement('div');
scratch.appendChild(placeholder);
const App = () => (
<div>
New content
<button>Update</button>
</div>
);

render(<App />, scratch, placeholder);
expect(scratch.innerHTML).to.equal(
'<div>New content<button>Update</button></div>'
);

render(<App />, scratch, placeholder);
expect(scratch.innerHTML).to.equal(
'<div>New content<button>Update</button></div>'
);
});
});

describe('replaceNode parameter', () => {
function appendChildToScratch(id) {
const child = document.createElement('div');
Expand Down

0 comments on commit 3a54222

Please sign in to comment.