I'm trying to understand how ChangeDetectionStrategy.OnPush
is working. On many tutorials people say that when component has OnPush
strategy then component only depends on Inputs and also change detection won't happen for component's subtree is object passed to child components have the same reference.
And from my testing it works like that. But I'm wondering why modification of object is still reflected in parent components, even if they have also OnPush
strategy? It looks like component is calling something like markForCheck and parent components are marked to run change detection.
But why? Isn't OnPush
supposed to be used to tell that this component only depend on input properties from parent component? Why do change detection for parent component if this component is also OnPush
component?
Here is my sample app. When I click changeDataImmutable then all data in sub components change because there is new object created. But when I click changeDataMutable then child components are not changed. However when I click modifyX or modifyY which mutates some deeply nested properties then this changes are still reflected in parent component (component 1). Why?