How abstraction helps you get the most out of UI components
Reuse UIs by simplifying your components
Software maintenance consumes between 40β80% of a projectβs resources. The burden of maintenance affects both project velocity and team satisfaction. Wouldnβt you rather create new features than dredge through bugs and tech debt?
Module reuse classically lessens the cost of maintenance. The more you reuse the code youβve already built & tested the more robust your applications. When building user interfaces, itβs consistent and faster to reuse UI components.
However, components arenβt reusable by default. A core strategy to create reusable components is abstraction. That is, βseparating concernsβ of an idea from the context in which it was born.
An idiomatic pattern, the βpresentational/containerβ split abstracts the presentational part of a component out of its data context. The goal being a UI component whose code is simpler for other developers to understand.
However, to reuse a component, we also need to make it easier for a other apps to βunderstandββββie. consume. We need to abstract the use of a component from the app in which it was born.
Types of components
All components are not equal and not all components belong in a component library. In a previous article I described the difference between single-use and reusable components:
- Single-use components are confined to their current location. They intertwine app-specific concerns with their βcoreβ concerns.
- Reusable components can be applied in many situations. They do not rely on anything from the environment they render in. There is a clear delineation between presentation and data (or anything else that is environment-specific).
The defining characteristic of a reusable component is the ability to render in any environment, given the correct inputs. In other words itβs abstracted from the global state, styles, business logic, and data. This allows for βpresentationalβ or pure components to be reusable in different places within the same app and opens the door for portability to other apps.
This isnβt to say writing single-use components is bad; components that understand the app they are in are the glue that makes the whole thing work. Still, abstracting reusable components out where possible will help grow your library and encourage consistency.
What to do about abstraction
UI component libraries should only contain reusable components that work in isolation of the app context. These are generally pure + presentational UI components that are abstracted from the data context and app environment.
Example: CommentList component
CommentList
renders messages from various authors. Shown below are 3 permutations that rely on the same Star Wars data source. You might find a component like this confined to a galactic chat app. We want this component to be reusable in any app. Letβs make it so.
First, look at a non-reusable implementation of the component:
The component is non-reusable because itβs intertwined with database queries and assumes the presence of specific routes.
If youβve created this component in haste you might be tempted to pass fields directly from the database through to the component (our commentText
and characterName|Avatar|Id
fields). Although initially convenient, this makes the component awkward to use in other contexts. A better idea is to transform the data in your own app and make the componentβs API cleaner.
The component also prescribes how the route to the author should be constructed with <a href={`/character/${id}`}>{name}</a>
which requires developers to pass id as a property. This is poor for reuse because the assumption that an appβs environment is setup with a /characters
directory and routes are constructed with id
βs.
This works in instances where you have you database setup just so and your comments are limited to Star Wars characters
. However, there isnβt much flexibility for situations where you want to display comments from superheroes
or Twitter
users.
A reusable implementation relies only on inputs as props
to pass in data the component requires to render. It does not rely on a specific database fields or assume anything about app routes.
Your component is now ready for different data:
Final words
Abstraction is simplification. Not only is it best practice for a tidy frontend, it allows the flexibility required for component reuse. As you can see with the CommentList
example, small tweaks make a big difference in reusability. Itβs not hard to imagine a library consisting of Dropdown
, DatePicker
, and Drawer
components whose patterns are endlessly reused to reduce the maintenance burden on your team.
Chroma is a big proponent of component reuse. No one should have to rebuild the same UIs. Weβre exploring a world where frontends are assembled from reusable components and are excited about our findings. If that sounds interesting to you, sign up for the mailing list to discover more articles on the topic. Do us a favor and β€ this article.