Skip to content

Instantly share code, notes, and snippets.

@e-oz
Created March 19, 2024 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e-oz/99d04094abe5007d882f35879eb00da3 to your computer and use it in GitHub Desktop.
Save e-oz/99d04094abe5007d882f35879eb00da3 to your computer and use it in GitHub Desktop.

Use cases for effects

Effects are rarely needed in most application code, but may be useful in specific circumstances. Here are some examples of situations where an effect might be a good solution:

  • Logging data being displayed and when it changes, either for analytics or as a debugging tool.
  • Keeping data in sync with window.localStorage.
  • Adding custom DOM behavior that can't be expressed with template syntax.
  • Performing custom rendering to a , charting library, or other third party UI library.

When not to use effects

Avoid using effects for propagation of state changes. This can result in ExpressionChangedAfterItHasBeenChecked errors, infinite circular updates, or unnecessary change detection cycles.

Because of these risks, Angular by default prevents you from setting signals in effects. It can be enabled if absolutely necessary by setting the allowSignalWrites flag when you create an effect.

Instead, use computed signals to model state that depends on other state.

@e-oz
Copy link
Author

e-oz commented Mar 19, 2024

Screenshot 2024-03-20 at 00 50 18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment