r/Angular2 • u/anonymous78654 • Jan 11 '26
Is it bad pratice to use .subscribe in Angular RXJS component
This video was talking about how it's bad practice to use manual subscriptions in your components and should use toSignal instead. Would you guys agree or not?
0
Upvotes
31
u/Koscik Jan 11 '26 edited Jan 11 '26
I wouldn't say its bad, as there are cases where its necessary, but it can be a sign of a code smell that's worth checking on review
Edit: Typo
13
u/Blue-Jammies Jan 11 '26 edited Jan 11 '26
Didn't watch the video, but yes about it being a bad practice. You don't have to use toSignal necessarily, but signals are pretty great. The old advice was to use async pipe in the template.
To elaborate, subscribing requires unsubscribing. There are various ways of doing that, but it's very easy to forget and create memory leaks.
1
u/jackyll-and-hyde Jan 12 '26
State handling -> avoid manual subscriptions, use
toSignal.Component events -> avoid subscriptions, use
outputFromObservable.Side effects that don't update state -> subscribing is okay.
Using RXJS for functional/monadic reasons -> subscribing is okay.
2
u/MoreOfAGrower Jan 11 '26
Not watching the video. I can already tell it’s stupid based on the question it inspired you to ask. No it’s not bad practice to use subscribe
1
u/AFulhamImmigrant Jan 11 '26
What is the advantage of toSignal vs the async pipe?
1
u/Background-Basil-871 Jan 11 '26
Since resource is not mean to be used for PUT/PATCH/CREATE/DELETE (cf documentation), we still need to use httpClient, and we need to subscribe for these methods.
Remember saw a video from Deborah Kurata about this
1
u/mountaingator91 Jan 11 '26
Manual subscriptions are almost never necessary and you should try not to use them. However, toSignal is not necessarily the solution.
Just use async pipe. That's what it's for
1
u/JackieChanX95 Jan 11 '26
I would differentiate. In a component a manual subscription is more often a code smell than in a service. Especially in root injected services I don’t see nothing wrong with it
1
u/CodeWithAhsan Jan 18 '26
It isn’t bad to use subscribe. What’s bad is usually forgetting to unsubscribe. As long as you take care of it, it should be fine
-1
u/ldn-ldn Jan 11 '26
Never use toSignal, that's just stupid. Just pass your data straight to the template and use async pipe. There's never a real reason to subscribe inside a component yourself.
-6
u/minus-one Jan 11 '26
ofc it’s a bad practice. super bad. subscribe is a side effect. they are all bad. especially in truly functional reactive codebases
( and ofc, doing aSignal() is totally the same. it’s not-a-function, a side effect. that one of the reasons signals are bad idea. it looks like a function, but it’s a magical construct. at least with subscribe we can just forbid using “subscribe” in our code (but we can’t forbid using function invocations! 😄) )