Day 22 of 100days of code

Hello and welcome to day 22 of my challenge!💪💪

How to Split UI into Components

Splitting UI into components depends on several factors, including:

Logical Separation of Content/Layout: Does the component contain pieces of content or layout that don't belong together? Identify distinct sections or elements in your UI representing independent pieces of functionality or content. Create components based on these logical separations. For example, create separate components for the header, navigation bar, main content area, and footer.

Reusability: Is it possible to reuse a part of this component? Identify UI elements appearing in multiple places across your application. Create reusable components for these elements to avoid duplication and enhance code maintainability.

Responsibilities/Complexity: Is the component doing too many different things? Does it rely on too many props, or have too many pieces of state and/or effects? Ensure components have clear responsibilities and are not overly complex. Break down complex components into smaller, more manageable pieces when necessary.

Personal Coding Style: Do you prefer smaller functions/components? .However, the decision should always be context-dependent, and the key is to strike a balance that best serves the clarity and maintainability of the codebase.

Suggestion💡: When in doubt, start with a relatively large component and split it into smaller pieces when it becomes relevant.

Some General Guidelines:

  • Beware that creating a new component introduces a new abstraction, which comes with a cost. More abstraction requires more mental energy to switch between components, so avoid creating new components too early.

  • Name a component according to what it does or displays, and don't hesitate to use a long component name.

  • Co-locate related components within the same file; don't separate components into different files too early.

  • It's normal for an application to have components of different sizes, including both small and large ones.

Component categories

There are three main component categories:

  1. Stateless/Presentational Components:

    • These components have no internal state.

    • They receive and present data or other content.

    • Typically small and reusable.

    • Example: Button, Card, Header.

  2. Stateful Components:

    • These components have internal state.

    • They can still be reusable, depending on the nature of the state.

    • Example: Form, Modal, Carousel.

  3. Structural Components:

    • These components include pages, layouts, or screens of the app.

    • They are the result of composition of other components.

    • Can be large and non-reusable due to their specific role in organizing the overall structure of the application.

    • Example: HomePage, Dashboard, AppLayout.

Component Composition

  • Involves combining different components using the children prop or explicitly defined props.

  • This allows for the creation of more complex and flexible components by assembling simpler ones.

Use Cases for Component Composition:

  • Highly reusable components: Component composition enables the creation of components that can be reused in various contexts, promoting modularity and maintainability.

  • Fixing prop drilling: Prop drilling occurs when props need to be passed through multiple layers of components. Component composition helps mitigate this issue, making it easier to manage and pass props directly to the components that need them.

  • Great for layout: Component composition is particularly useful for creating flexible and dynamic layouts, allowing components to be combined and arranged to achieve different visual structures.

    Tomorrow, I'll work on a practical example of splitting UI into components.💪💪

  • Thank you for reading.🙏