Skip to content

Commit

Permalink
Skip over function children when rendering (#3919)
Browse files Browse the repository at this point in the history
Function children that are passed as props to a component are still allowed but if that Component doesn't invoke the function and passes it a child, we'll skip over it.
  • Loading branch information
andrewiggins authored Mar 2, 2023
1 parent 5b7fb23 commit 15f0c96
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/diff/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ export function diffChildren(
for (i = 0; i < renderResult.length; i++) {
childVNode = renderResult[i];

if (childVNode == null || typeof childVNode == 'boolean') {
if (
childVNode == null ||
typeof childVNode == 'boolean' ||
typeof childVNode == 'function'
) {
childVNode = newParentVNode._children[i] = null;
}
// If this newVNode is being reused (e.g. <div>{reuse}{reuse}</div>) in the same diff,
Expand Down
10 changes: 10 additions & 0 deletions test/browser/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,16 @@ describe('render()', () => {
expect(scratch.innerHTML).to.equal('<div></div>');
});

it('should not render children when rerendering a function child', () => {
const icon = () => {};

render(<div>{icon}</div>, scratch);
expect(scratch.innerHTML).to.equal('<div></div>');

render(<div>{icon}</div>, scratch);
expect(scratch.innerHTML).to.equal('<div></div>');
});

it('should render NaN as text content', () => {
render(NaN, scratch);
expect(scratch.innerHTML).to.equal('NaN');
Expand Down

0 comments on commit 15f0c96

Please sign in to comment.