Z
Z
zlodiak2018-11-04 14:38:35
JavaScript
zlodiak, 2018-11-04 14:38:35

Why doesn't the OnChanges() hook fire when the onPush strategy is disabled?

I'm trying to work with the onPush strategy and I see strange results. Please help me figure it out. I will show 2 pages, there is an error in only one of them.
On THIS page, I use the onPush strategy and get the expected result:

  1. in the onpush-object.component component, when initializing the input variable msg, a value is assigned.
  2. then the parent component overwrites the value of this input variable. And it is the object that is overwritten, not its property.
  3. accordingly, this results in an output to the console because the OnChanges() hook is triggered
  4. further, when changing the property of the input object in the template, nothing changes because onPush is active
  5. for the same reason, the OnChanges hook does not work now

On THIS page, I'm doing the same, but with the onPush strategy disabled.
  1. Accordingly, I expect that when the property of the incoming object changes in the template, there will be changes (it is).
  2. and expect the OnChanges() hook to fire. But it doesn't work. This is not clear to me.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Stroykin, 2018-11-04
@zlodiak

The reference to your object does not change. The OnChanges hook only fires the first time in both examples and doesn't fire again because the reference to the object doesn't change, since it's still the same object, even if the content of the object has changed. In the second case, the template is redrawn because the onPush strategy is disabled. In order for the object changes to be processed during the onPush strategy, there are 2 options: create a new object or use the DoCheck hook and write the change detection logic yourself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question