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

signal - effect() not always triggered when the signal is updated #51180

Closed
rlejeune opened this issue Jul 26, 2023 · 4 comments
Closed

signal - effect() not always triggered when the signal is updated #51180

rlejeune opened this issue Jul 26, 2023 · 4 comments
Labels
area: core Issues related to the framework runtime core: reactivity Work related to fine-grained reactivity in the core framework needs reproduction This issue needs a reproduction in order for the team to investigate further
Milestone

Comments

@rlejeune
Copy link

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

Not sure if this is a bug, or if I use effect() wrong.

I have this effect in the constructor, I tried with patchValue too btw same result.

effect(() => {
   this.organizationForm?.controls['name'].setValue(this.organization().name);
});

And this code which is called when the form is saved, which should trigger the effect but it does not

const organization = await this.service.saveOrg();
this.organization.update(() => organization);

If I change the code in the effect for this, then it is triggered.

effect(() => {
   const name = this.organization().name;
   this.organizationForm?.controls['name'].setValue(name);
});

Any reason why assigning the signal value to a const first before setting the form value works, but not if used directly?

Thank you!

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 16.1.5
Node: 16.16.0
Package Manager: npm 9.7.2
OS: linux x64

Angular: 16.1.7
... animations, common, compiler, compiler-cli, core, forms
... localize, platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1601.5
@angular-devkit/build-angular   16.1.5
@angular-devkit/core            16.1.5
@angular-devkit/schematics      16.1.5
@angular/cli                    16.1.5
@schematics/angular             16.1.5
rxjs                            7.8.1
typescript                      5.1.6
zone.js                         0.13.1

Anything else?

No response

@pkozlowski-opensource
Copy link
Member

Could you please share a reproduction in a stackblitz? It is kind of hard to understand what is going on here without more details. I do have some "suspect areas" but again, impossible to say without a reproduce scenario.

@pkozlowski-opensource pkozlowski-opensource added needs reproduction This issue needs a reproduction in order for the team to investigate further area: core Issues related to the framework runtime core: reactivity Work related to fine-grained reactivity in the core framework labels Jul 26, 2023
@ngbot ngbot bot added this to the Backlog milestone Jul 26, 2023
@rlejeune
Copy link
Author

While trying to reproduce it, it looks like the problem is caused by a third party lib, ng-zorro-antd.

I cannot reproduce it without the lib

Sorry for your time and thanks!

@AndrewKushnir AndrewKushnir closed this as not planned Won't fix, can't repro, duplicate, stale Jul 26, 2023
@JoostK
Copy link
Member

JoostK commented Jul 26, 2023

To provide some additional insight:

this.organizationForm?.controls['name'].setValue(this.organization().name);

The above usage of optional chaining may bail the execution of the remainder of the expression (if this.organizationForm is undefined/null) which prevents the argument list to setValue from being evaluated. As such, the signal would not be read and no dependency would be registered, hence the effect would never rerun.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Aug 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: core Issues related to the framework runtime core: reactivity Work related to fine-grained reactivity in the core framework needs reproduction This issue needs a reproduction in order for the team to investigate further
Projects
None yet
Development

No branches or pull requests

4 participants