Skip to content

Commit f10b694

Browse files
liujupingJackLian
authored andcommitted
feat(renderer-core): optimize the judgment of whether leaf hoc has children
1 parent 0e90ea8 commit f10b694

2 files changed

Lines changed: 43 additions & 9 deletions

File tree

packages/renderer-core/src/hoc/leaf.tsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -521,16 +521,11 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
521521
}
522522

523523
get hasChildren(): boolean {
524-
let { children } = this.props;
525-
if (this.state.childrenInState) {
526-
children = this.state.nodeChildren;
527-
}
528-
529-
if (Array.isArray(children)) {
530-
return Boolean(children && children.length);
524+
if (!this.state.childrenInState) {
525+
return 'children' in this.props;
531526
}
532527

533-
return Boolean(children);
528+
return true;
534529
}
535530

536531
get children(): any {
@@ -576,7 +571,11 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
576571

577572
delete compProps.__inner__;
578573

579-
return engine.createElement(Comp, compProps, this.hasChildren ? this.children : null);
574+
if (this.hasChildren) {
575+
return engine.createElement(Comp, compProps, this.children);
576+
}
577+
578+
return engine.createElement(Comp, compProps);
580579
}
581580
}
582581

packages/renderer-core/tests/hoc/leaf.test.tsx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,41 @@ describe('onChildrenChange', () => {
504504
DivNode.emitChildrenChange();
505505
makeSnapshot(component);
506506
});
507+
508+
it('children is 0', () => {
509+
DivNode.schema.children = 0
510+
DivNode.emitChildrenChange();
511+
const componentInstance = component.root;
512+
expect(componentInstance.findByType(components.Div).props.children).toEqual(0);
513+
});
514+
515+
it('children is false', () => {
516+
DivNode.schema.children = false
517+
DivNode.emitChildrenChange();
518+
const componentInstance = component.root;
519+
expect(componentInstance.findByType(components.Div).props.children).toEqual(false);
520+
});
521+
522+
it('children is []', () => {
523+
DivNode.schema.children = []
524+
DivNode.emitChildrenChange();
525+
const componentInstance = component.root;
526+
expect(componentInstance.findByType(components.Div).props.children).toEqual([]);
527+
});
528+
529+
it('children is null', () => {
530+
DivNode.schema.children = null
531+
DivNode.emitChildrenChange();
532+
const componentInstance = component.root;
533+
expect(componentInstance.findByType(components.Div).props.children).toEqual(null);
534+
});
535+
536+
it('children is undefined', () => {
537+
DivNode.schema.children = undefined;
538+
DivNode.emitChildrenChange();
539+
const componentInstance = component.root;
540+
expect(componentInstance.findByType(components.Div).props.children).toEqual(undefined);
541+
});
507542
});
508543

509544
describe('not render leaf', () => {

0 commit comments

Comments
 (0)