angular / angular/components

bug(mat-tree): childrenAccessor is not lazily evaluated and leaf nodes are not filtered out before calling the childrenAccessor

Open
#31,289 1 comment 6 reactions 0 assignees View on GitHub
area: material/tree P2
Dominant language
TypeScript
Stars
25k
Forks
6.8k
Avg merge
1d 8h
Merged PRs (30d)
91

Description

### Is this a regression?

- [ ] Yes, this behavior used to work in the previous version

### The previous version in which this bug was not present was

_No response_

### Description

`childrenAccessor` should only be called when expanding a node.

In the current state, workarounds are needed to support a mat-tree which asynchronously fetches children. If the dataset is large enough, there will then be a proportionately large number of fetches for the children.

### Reproduction

StackBlitz link: [stackblitz](https://stackblitz.com/edit/angular18-tree-duplicates-6iwee3dq?file=src%2Fmain.ts)

Notice how the tree eagerly fetches all children, including children of leaf nodes. The number on the bottom of the page indicates the number of times childrenAccessor was called.

### Expected Behavior

`childrenAccessor` should only be called when expanding a node.

Moreover, it should also *not* call the childrenAccessor for nodes which are not expandable, since we already provide the `when: hasChild` predicate.

### Actual Behavior

`childrenAccessor` is eagerly called for each node when the tree is created, including leaf nodes.

### Environment

- Angular: 20
- CDK/Material: 20
- Browser(s): Chrome
- Operating System (e.g. Windows, macOS, Ubuntu): MacOS

Contributor guide

Open the contributing guide

Research direction

Start with the linked StackBlitz reproduction at src/main.ts and verify the childrenAccessor call count for expandable and leaf nodes. Trace the mat-tree implementation responsible for those calls, then confirm that calls occur only on expansion and are skipped for nodes rejected by the when predicate.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.