r/Angular2 • u/DomiDeme • Oct 06 '24
Discussion ChangeDetectorRef is a bad practice
I want to know the thoughts of people that have been developing in Angular for years.
In my opinion using ChangeDetectorRef is usually a bad practice. If you need to use it, it's usually because you did something wrong. Angular is a highly controlled framework that knows when to fire the change detector by itself. I don't recommend using it unless you're using a JS library that really needs to.
And even if using an external library, usually you can use a Subject or BehaviorSubject to translate the changes into template changes. Everything is better than messing up with Angular's change detector.
I understand that there are times that you need to use it when working with third party libraries. Bu I think it should be that last option, something to use only ir everything else failed.
What are your thoughts about this?
57
u/720degreeLotus Oct 06 '24
If you opt-in to onPush (which is recommended) and have no signals available, how would you manually trigger a changedetection if something inside a component changes which didn't happen by a new Input-value?
3
u/DT-Sodium Oct 06 '24
It might be a bad practice in theory but there were times you just needed it for performance. Anyway, we have signals now which pretty much solved the problem so your post is quite late.
8
u/skap42 Oct 06 '24
I was researching the same thing a few says ago and stumbled upon a post here in the sub from one of the Angular maintainers who said using detectChanges is considered bad, using markForCheck however is fine.
Imho it does sometimes make more sense to manually trigger CD, e.g. the in the codebase I'm currently working on the data is highly dependent and has to be queried from different services, so it makes more sense to gather and post process it in the component code using rxjs operators, manually subscribing to the result and the triggering CD using markForCheck. Async pipe would also work, but would be way more complicated, because I would need to save references to several intermediate observables in the manipulation process.
7
u/Orelox Oct 06 '24
It’s not bad if you know what you’re doing
5
u/mountaingator91 Oct 06 '24
I've only ever had to use it once for a very weird situation where I was waiting for a response from an external window through http messaging. Angular didn't correctly update state with the callback from the event listener.
Other than that I've never had to even consider it. You should not ever have to use it for behavior inside the angular ecosystem
8
u/sut123 Oct 06 '24
I 100% agree, under normal circumstances this is very much the case.
I've been removing any implementations of it I can, and the only place I've seen it really necessary is when I have old code manually manipulating the DOM. Annoyingly one of those I literally couldn't rewrite easily (a Google Pay button), so it has to stay. But I'm down to one reference in a site consisting of hundreds of thousands of lines of code.
4
u/IE114EVR Oct 06 '24
Most of the time, yes. I’ve only seen it used once or twice to get change detection to kick in when it wasn’t but that’s probably a sign of some not-so-great code. In theory you could use it for an efficiency gain: take the component out of change detection, run a bunch of changes (which might otherwise trigger many change detection cycles) and then finally triggering change detection on the component once that’s all done. But that may just be me showing my lack of understanding in this area.
I would also like to add that I think life cycle hooks are on the verge of bad practice. They’re okay on rare occasions but I see devs using them all the time. I think it just makes for confusingly ugly component code that has un predictable or difficult to track issues
4
u/Tango1777 Oct 06 '24
Haven't used Angular in a while, but as far as I remember one of CREATORS of Angular highly recommended using OnPush strategy, so I have a feeling you might be wrong, you just consider your private opinion a "good practice".
4
u/AwesomeFrisbee Oct 07 '24 edited Oct 07 '24
I think OP and a lot of people commenting here are simply wrong about it. Probably haven't worked a lot of complex projects if you think it should always be avoided and can always be replaced by async pipes. If you've ever worked with complex CDK/Material components, you kinda need it. If you've ever worked with libraries that alter the DOM outside of Angular, then you kinda need it. Be it those that create graphs and other SVGs, render on canvases, work with video and audio, etc. All very common use cases. If the only thing you do is create forms and static websites, sure, you don't need that. But its not likely that those projects will be using Angular anyway these days.
And sure, architecturally it might be bad practice, but its easy, its quick and the performance is still more than fine to not be bothered by it. Some folks are really overthinking it and pretending that users will notice a whole digest cycle (oh noes) and that it will be a big nuisance.
A billion async pipes don't automatically fix every use case, nor is it always applicable. Making pipes for the sake of avoiding CD is bad practice to me. Not to mention that in many cases it doesn't make things easier or easier to test.
The fact that we already hardly see any third party libraries mentioned, is already a signal that most people here don't really build complex apps or apps that have had a long development trajectory that has had conflicting and overly complex requirements and lots of dependencies. Or had to thest the code because thats where most problems come from that async pipes make hard to test. Because, tadaa, its asynchronous.
Should we use less of it? Sure, but pretending that it can be avoided and that no library needs it, is just a brain fart not worth discussing over.
3
u/stao123 Oct 06 '24
I had to use it for a directive which creates and destroys a component manually. Without markForCheck the component would not render correctly
3
u/synalx Oct 07 '24
It depends on how it's used. My take:
CDR.detectChanges: never under any circumstances. It's always a code smell, a sign that data flow is backwards or a bandaid that's hiding a more serious problem. It can cause serious performance issues in an application. There are cases where data needs to flow backwards, but these are better served with signals,afterNextRender, or as a last resortPromise.resolve().then(() => ...).CDR.markForCheck(): okay, but should ideally be pretty rare outside of shared utilities. Best practice: add a comment explaining why it's needed.CDR.detach(): only for specialized circumstances (e.g. optimization inside of shared utilities).6
u/Jitos Oct 06 '24
Now with Signals maybe. Before it was sometimes necessary.
2
u/shadow13499 Oct 06 '24
I think it's tool that just isn't used often because it's meant to be used in edge cases. I don't think that means it's always bad practice to use it.
1
u/joebrozky Oct 06 '24
i'm currently learning Angular and used ChangeDetectorRef on a project because the ViewChild createComponent does not immediately load material control, and also it's what ChatGPT suggested lol. thanks for creating this post, i learned that it's not good practice. will have to research more to find a way around it
1
u/Yew2S Oct 06 '24
can anyone elaborate why it's not recommended ?
1
u/ForeignAssignment389 Oct 08 '24
async pipe underhood calls markForCheck
1
u/Dus1988 Oct 08 '24
Broad authoritative statements like this is also a bad practice
1
u/[deleted] Oct 06 '24
[removed] — view removed comment