Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for You may not need ngOnChanges
Hank Chiu
Hank Chiu

Posted on • Edited on

     

You may not need ngOnChanges

"ngOnChanges" is a lifecycle hook for an Angular component to know when the @Input props are changed. The main drawback of using ngOnChanges is that you have to write much more code to watch a single prop.

Angular team also provides another way tointercept the property changes by setter. If you use the setter technique naively you would find it tediously to write the getter/setter pair and the redundant private variable.

In this article, I would like to share how I improve the setter technique into an npm module -subjectize.

Usage

Say we are building a counter component and would like to do something whenever the count changes. We could have 3 versions of implementation like below(excerpt):

1) By ngOnChanges

classCounterComponentimplementsOnChanges{@Input()count:number;ngOnChanges(changes:SimpleChanges){if(changes.count){// do something}}}
Enter fullscreen modeExit fullscreen mode

2) By naive setter

classCounterComponent{@Input()getcount():number{returnthis._count;}setcount(value){this._count=value;// do something}private_count:number;}
Enter fullscreen modeExit fullscreen mode

3) By Subjectize

classCounterComponentimplementsOnInit{@Input()count:number;@Subjectize("count")count$=newReplaySubject(1);ngOnInit(){this.count$.subscribe(()=>{// do something});}}
Enter fullscreen modeExit fullscreen mode

They may look fair in such a simple scenario, but things go differently when you have a few Input props to watch. For ngOnChanges, you got lots if logics. For naive setter, you got many boring private variables.

The Subjectize is also a mentally-direct approach: declare a RxJS Subject and subscribe to the Subject for changes, that's it.

The magics

The Subjectize is a TypeScript property decorator. Under the hood, it creates an internal getter/setter for the specified Input prop, just like the naive setter implementation. The Subjectize itself only depends on RxJS, hence you can use it on any ES6 class without Angular. You could also use it for simple state management.

Without saying, there are more things to do to keep the reliabilities. If you are interested, see thesource code.

Conclusion

JavaScript getter/setter can be used to watch Input props andsubjectize helps you to do that. If you just got fed up with ngOnChanges, givesubjectize a try!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I help companies refactor/rearchitect their codebase to make it scalable, maintainable and readable.
  • Location
    Taiwan
  • Work
    Web developer
  • Joined

More fromHank Chiu

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp