GithubHelp home page GithubHelp logo

Comments (5)

Eraldo avatar Eraldo commented on June 19, 2024

Referenced in Ionic: ionic-team/ionic-framework#29444

from angular.

Flo0806 avatar Flo0806 commented on June 19, 2024

Hello. We found the same issue in a new project inside our company. It looks like the problem is really the changeDetectorRef.detach. This simple component isn't shown in effect too, if you add the same directive:

import { ChangeDetectionStrategy, ChangeDetectorRef, Component } from '@angular/core';

@Component({
  selector: 'app-test-button',
  standalone: true,
  imports: [],
  templateUrl: './test-button.component.html',
  styleUrl: './test-button.component.css',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class TestButtonComponent {
constructor(changeRef: ChangeDetectorRef) {
  changeRef.detach();
}
}

Component and directive:

@Directive({
  selector: '[test]',
  standalone: true,
})
export class TestDirective {
  test = input.required<string>();
  test2 = computed(() => {
    return this.test() + '2';
  });

  constructor() {
    effect(() => {
      console.log('>> EFFECT', this.test2());
    });
  }
}

.........

<app-test-button [test]="inputText">
    Button with Directive
</app-test-button>

from angular.

JeanMeche avatar JeanMeche commented on June 19, 2024

The root cause is the injection of the ChangeDetectorRef in directives. When the directive is applied to a component, the CDR injected in the directive is the components CDR.

The effect is scheduled to run first when the component is init and uses the CDR to do that. In this case, the component is never init (because of running detach in the constructor) so the effect is never started.

A workaround for this would be to let the component finish its initialization and detach only in the ngAfterViewInit hook. This way the effect will

from angular.

pkozlowski-opensource avatar pkozlowski-opensource commented on June 19, 2024

Here is a minimal repro: https://stackblitz.com/edit/stackblitz-starters-7zd1fv?file=src%2Fmain.ts

As @JeanMeche pointed out, the root cause here is a component detaching from the change detection tree. Having said this I do recognize that this is very non-obvious - this is one of the cases we want to look into before stabilizing effects API.

from angular.

pkozlowski-opensource avatar pkozlowski-opensource commented on June 19, 2024

I've created #55808 to capture the root cause without any external dependencies. Let's continue the discussion in there.

Duplicate of #55808

from angular.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.