From be004b89fc7938b5a020a743e3344268601d0e9f Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Wed, 18 Sep 2024 18:41:47 +0200 Subject: [PATCH] fix(cdk/tree): resolve maximum call stack error (#29754) The CDK tree was calling `ChangeDetectorRef.detectChanges` recursively for each node in the tree which was overflowing the call stack with some larger trees. These changes resolve the issue by only calling `detectChanges` on the root. Fixes #29733. --- src/cdk/tree/tree.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/cdk/tree/tree.ts b/src/cdk/tree/tree.ts index fe183fa8ea66..02b4f1bbbc83 100644 --- a/src/cdk/tree/tree.ts +++ b/src/cdk/tree/tree.ts @@ -551,9 +551,15 @@ export class CdkTree } }); - // TODO: change to `this._changeDetectorRef.markForCheck()`, or just switch this component to - // use signals. - this._changeDetectorRef.detectChanges(); + // Note: we only `detectChanges` from a top-level call, otherwise we risk overflowing + // the call stack since this method is called recursively (see #29733.) + // TODO: change to `this._changeDetectorRef.markForCheck()`, + // or just switch this component to use signals. + if (parentData) { + this._changeDetectorRef.markForCheck(); + } else { + this._changeDetectorRef.detectChanges(); + } } /**