So, I'm still struggling to decide what makes a good component during the development of a web application. How do I know if my component is too big? or if it is too small? Does a component always have to be developed with reusability in mind?
It would be a great help if there is some good learning resource to teach patterns in Components to understand the DOs and DONTs when creating a new component.
Top comments (4)
(I'm an Angular dev that cares about code quality and will provide my opinion of what makes a good component, from a mix of a few articles and from some experience 😄)
TLDR: There is no such thing as a "good component" in general, its all based upon what your doing, but smaller is usually better.
I'd say there are 2 types of components:
This component could get large, but you want to delegate to other entities as much as possible, leaving just "big picture" stuff to this component. These entities could be external functions/helper-classes or sub "smart" components.
You could create a smart component inside another smart component, but I'd only consider this for complex views with multiple parts that are separate. Generally you will run into issues communicating between smart components that are "siblings", and this is where state-management solutions like Redux/ngrx come in, or solutions like React Hooks. If you don't use these solutions, I'd be very careful creating multiple sibling components without a solid game-plan on how to communicate between them.
The issues with these concepts is its hard to know which one to use most of the time, and handle all the edge cases. Its very easy to just make everything "smart", but then you end up having a hard time re-using anything.
You can't make all components dumb because you will eventually need some logic otherwise your app does nothing 😉
The key is to focus components, of any complexity, on primarily displaying data and managing minimal state. These sort of approaches are founded in a lot of state-management solutions, which pull away responsibility from the components themselves. (here's the motivation behind Redux) You should always strive to make all components simpler regardless of framework, or if your are or aren't using a state-management system.
Finally I want to point out as an Angular dev I can say an Angular component can be backed up by services (common logic, message passing), injection-tokens (customization via DI), directives (template logic) and pipes (template logic), which remove a number of use-cases from component logic.
I have limited knowledge of React, but do know JSX is far more flexible, so the presentation of complex UI is easier in React than it is for Angular. (no idea where VueJS stands here haha)
Keep things simple, and move logic out of components when and where possible. Isolate state out of components and most problems and complexity should go away. :)
extra reading:
Primary sources (framework docs):
reactjs.org/docs/hooks-intro.html
reactjs.org/docs/components-and-pr...
angular.io/guide/component-interac...
Secondary sources (blogs):
blog.angular-university.io/angular...
itnext.io/react-component-class-vs...
I didn't expect to have such a complete answer
Your comment is pure gold
This is a great question and not one that many feel comfortable asking.
Just like any software architecture decision, there's almost never a right or wrong answer. It's a skill that you'll get better with over time. I think of it as intuition-based skill. Here are some of the lessons that I have learned that may help with learning.
Take a small amount of time (actual amount will depend on the size of the feature) and think about your architecture before you write any code.
Imagine what is likely/unlikely to change.
Avoid Paralysis by Analysis.
Reflect on every decision post-implementation.
I highly recommend Clean Code by Bob C. Martin & Pragmatic Programmer by Andrew Hunt & David Thomas. Both are really great books on this topic.
I hope that this helps.
Thats some really good tips there, thanks for sharing.