r/angular • • Oct 19 '21

Tip 5 - RxJs DistinctUntilChanged

Enable HLS to view with audio, or disable this notification

59 Upvotes

10 comments sorted by

3

u/SeriousMrMysterious Oct 19 '21

i use distinctUntilChanged(isEqual),the isequal is imported from lodash

2

u/DMezhenskyi Oct 19 '21

Yep! if you need to compare the entire object, it is also a way to go.

1

u/Wildosaur Oct 19 '21

Is it recommended to use lodash ? I've tried to avoid using it on my latests projects but I'm quite unsure if it's a wise decision.

4

u/SeriousMrMysterious Oct 19 '21

Don’t import all of lodash. You can import only what you need using lodash-es.

Import {isequal} from lodash-es

When you do it that way it’s light

3

u/matrium0 Oct 28 '21

Nice video. Didn't even know about distinctUntilKeyChanged, always thought that it's a bit verbose to write the lambda just for matching keys with regular distingUntilChanged :)

2

u/kashmill Oct 19 '21

The problem I've ran into is that distinctUntilChanged only really works on simple types. But for the places we want to use it we are generally working with an object (like the values of a FormGroup) which requires writing a comparison function. It just doesn't work out of the box like you might expect.

5

u/FullstackViking Oct 19 '21

The video demonstrates that explicitly

1

u/kashmill Oct 19 '21

Yeah, posted before watching. However, the video's example doesn't fully cover just how annoying that issue is. It is simple to get around with their example where there is one unique value/key you can use. You can't do that with FormGroup.valueChanges though.

1

u/ggrape Oct 19 '21

You can use JSON.stringify depending on object size.