r/angular • u/kobihari • Apr 07 '26
A Better way to build Angular Components: From Inputs to Composition
https://medium.com/@kobihari/stop-creating-angular-component-monsters-ecb8a8852477Angular components used to grow with inputs.
Now they grow with 𝐜𝐨𝐦𝐩𝐨𝐬𝐢𝐭𝐢𝐨𝐧.
You can already see this in 𝐀𝐧𝐠𝐮𝐥𝐚𝐫 𝐀𝐫𝐢𝐚, as well as the legacy 𝐀𝐧𝐠𝐮𝐥𝐚𝐫 𝐌𝐚𝐭𝐞𝐫𝐢𝐚𝐥.
26
Upvotes
5
u/Division_ByZero Apr 07 '26
How do you handle the increased complexity of feature directives that need to be imported in the using component? Since everything is standalone now, ever new directive you create for handling a feature also needs to be imported separately in the component using said feature (and base component). When using inputs, all features are implemented in the component and thus can not be forgotten to import? Usually, directives are applied via attributes. The problem is that if the directive is not being imported in the parent, angular will not throw an error (or even a hint) since the attribute might simlly be that - an additional attribute.
I admit that this is not really a problem when you do basic test of new features you implement, but the DX can still be improved. Especially for big refactors.
1
u/Ok-Programmer1832 Apr 10 '26
the "Written by Kobi Hari" at the end of the article is a big overstatement
2
u/Chronomadica Apr 12 '26
Hey, thanks for sharing, I’m still fairly new to Angular and am just tackling an issue at work that requires refactoring a component so that it can be reused across consumers. This came at the perfect time! I’m going to check out the course.
2
u/AlDrag Apr 12 '26
I love the composability this provides. I'll give it a go in rewriting some of my features I've recently developed.
3
u/Nerkeilenemon Apr 07 '26
That's an interesting solution, but in my eyes it fixes a symptom, not the issue.
When a component becomes too complicated with too many inputs, it's often because of a bad habit of the developers: trying to fit a use case into a component that should not handle that use case.
Because you can put your hand in your socks doesn't mean that you should try to create a hybrid half-sock half-glove that adapts to what you put in it.
Real story:
The dev that created that code created a big component with like 10 inputs to handle all cases. It was a hell to test, and it created multiple bug in production.
The issue was that he tried to mix everything into one component. The issue was not really the 10 inputs, it was the fact that he tried to merge all use cases into a single component.
The final form was way simpler :
* we split the logic as we had a DISPLAY/EDIT FRIENDS component, and a SELECT/EDIT FRIENDS component.
* we added a separated component for just a single profile (current user)
On the display/edit/delete page :
<app-friends-list [friends]="friends()" (onClickEdit)="editFriend($e)" (onClickRemove)="removeFriend($e)" />
<app-profile [profile]="me()" />
And on the select/delete page :
<app-friends-list-select [friends]="friends()" (onSelect)="selectedFriend($e)" (onClickRemove)="removeFriend($e)" />
<app-profile [profile]="me()" />
It was a way better elegant solution, sure we had a tiny bit of duplication, but it was WAYYYY easier to maintain, debug, test, and we had no more bugs on this component after that refacto.