Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error using Zoneless change detection #1015

Open
spock123 opened this issue May 7, 2024 · 6 comments
Open

Error using Zoneless change detection #1015

spock123 opened this issue May 7, 2024 · 6 comments

Comments

@spock123
Copy link

spock123 commented May 7, 2024

Hi,

  • Toastr works great - however when adding the new Zoneless changedetection in Angular 18, I get this error when trying to create a toast:

Error: NG0100: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value for 'display': 'none'. Current value: 'undefined'. Expression location: _Toast component.

Any ideas what it could be? Thanks

As soon as I enable normal zoneJs again, it works without errors

@spock123
Copy link
Author

spock123 commented May 7, 2024

After looking at the source code I guess it's due to the use of NgZone in the. toastr.component.ts file.

@spock123
Copy link
Author

spock123 commented May 7, 2024

Back again - sorry for the spam. I actually think the reason is that , due to Angular/Fire issues, we had to keep ZoneJS in the angular.json polyfills, and still use the zoneless change detector.

For some reason this seems to mean ngx-toastr thinks ZoneJS is present, even though there is no change detection running.

Would it be a big task to make wether to use zoneJS or not, a configuration setting when providing the module?
THen we could manually specify wether to use zoneJS or not, instead of relying the library to "guess".

@kemotx90
Copy link

kemotx90 commented May 27, 2024

I think the best approach is fully remove Zone.js dependency from ngx-toastr

As Angular moves towards a zoneless application development model, Zone.js is no longer accepting new features, including additional patches for native platform APIs.
https://www.npmjs.com/package/zone.js#development-status-of-zonejs

@spock123
Copy link
Author

@kemotx90 I agree. In the mean time I switch to https://www.npmjs.com/package/@ngneat/hot-toast which doesn't use zone at all

@atscott
Copy link

atscott commented May 28, 2024

Hi, Angular team member here. This isn’t related to the component’s use of NgZone. The run and runOutsideAngular methods are fine to use in Zoneless. https://angular.dev/guide/experimental/zoneless#requirements-for-zoneless-compatibility

The error message indicates that issue is the toast component is not following the rules of OnPush change detection and updating state without marking itself for check. This host binding seems the likely cause:

if (this.state.value === 'inactive') {

This is a pretty straightforward fix: use signals for state read in templates or call remember to call ChangeDetectorRef.markForCheck when updating the state.

@kemotx90
Copy link

zone.js is under deprecation by the angular team.
Remove it from dependencies and use another approach is the best solution for LTS

junglerobba added a commit to junglerobba/ngx-toastr that referenced this issue May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants