Skip to content

Commit

Permalink
Fix _lastDomChild disregarding placeholders (#1587)
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister authored and JoviDeCroock committed May 1, 2019
1 parent 46329e9 commit f674187
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ export function diff(parentDom, newVNode, oldVNode, context, isSvg, excessDomChi
newVNode._dom = newVNode._children[0]._dom;

// If the last child is a Fragment, use _lastDomChild, else use _dom
p = newVNode._children[newVNode._children.length - 1];
newVNode._lastDomChild = p && (p._lastDomChild || p._dom);
// We have no guarantee that the last child rendered something into the
// dom, so we iterate backwards to find the last child with a dom node.
for (let i = newVNode._children.length; i--;) {
p = newVNode._children[i];
newVNode._lastDomChild = p && (p._lastDomChild || p._dom);
if (newVNode._lastDomChild) break;
}
}
}
else if (typeof newType==='function') {
Expand Down
34 changes: 34 additions & 0 deletions test/browser/fragments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1836,4 +1836,38 @@ describe('Fragment', () => {
rerender();
expect(scratch.innerHTML).to.equal(successHtml);
});

it('should use the last dom node for _lastDomChild', () => {
let Noop = () => null;
let update;
class App extends Component {
constructor(props) {
super(props);
update = () => this.setState({ items: ['A', 'B', 'C'] });
this.state = {
items: null
};
}

render() {
return (
<div>
{this.state.items && (
<Fragment>
{this.state.items.map(v => <div>{v}</div>)}
<Noop />
</Fragment>
)}
</div>
);
}
}

render(<App />, scratch);
expect(scratch.textContent).to.equal('');

update();
rerender();
expect(scratch.textContent).to.equal('ABC');
});
});

0 comments on commit f674187

Please sign in to comment.