6 Levels of Reusability

Rate this content
Bookmark

Master the art of making your components highly reusable. The 6 Levels of Reusability show us how we can progressively make our components more and more reusable — as needed — and include powerful patterns and tools to help us reuse our code more easily.

This talk has been presented at Vue.js Live 2024, check out the latest edition of this JavaScript Conference.

FAQ

Michael Thiessen is a full-time Vue educator who spends his time writing articles, sending out a weekly newsletter, creating courses, and giving talks.

The main focus of Michael Thiessen's talk is on reusable components, specifically how to write less code and make it more effective by making components reusable.

Reusable components are focused on efficiency and effectiveness, allowing developers to write less code and get more done. Clean components, on the other hand, are more about long-term maintainability.

The six levels of reusability mentioned in the talk are templating, configuration, adaptability, inversion, extension, and nesting.

The templating level involves reusing different parts of a template by encapsulating UI and logic inside components, making them reusable across different parts of an application.

Configuration props are used to create different variations of a single component's behavior. They often include boolean types and enum types, allowing toggling or selecting from a list of acceptable values.

The adaptability level uses slots to make components more flexible and adaptable to future use cases. This allows developers to expand what can be done in a component beyond what props allow.

Inversion of control refers to giving up control of the application flow to other code. In reusable components, this is often achieved through scoped slots, allowing flexibility by letting components manage their own behavior.

Extension points allow specific parts of a component to be overridden as needed, making the component more flexible and reusable by selectively customizing its behavior.

The nesting level involves nesting scoped and regular slots throughout the component tree, allowing for the reuse of intermediate components and maximizing reusability across the application.

Michael Thiessen
Michael Thiessen
23 min
25 Apr, 2024

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Today's Talk explores the concept of reusable components, focusing on levels of reusability such as inversion, extension, and nesting. Props are discussed as a means of achieving flexibility and configuration in components. Inversion of control and scoped slots are introduced to give components adaptability. Extension points provide the ability to override specific parts of an application within a single component. The Talk also covers the use of slots and nesting for component flexibility and reusability.
Available in Español: 6 Niveles de Reutilización

1. Introduction to Reusable Components

Short description:

Today we'll discuss reusable components and how they help us write less code while being more effective and efficient. The difference between reusable and clean components lies in maintainability versus reusability. I have developed a framework for determining the appropriate level of reusability for our components. We will focus on the last three levels of reusability: inversion, extension, and nesting. Level one is templating, where we reuse different parts of our template. The next level is configuration, where we create different variations of a single component.

Hey, my name is Michael Thiessen and today I'm going to be talking to you about reusable components. So if you don't know who I am, I'm going to give you just a little bit about myself before we get into this. I am a full time Vue educator, so that means that I spend all of my time writing articles, sending out my weekly newsletter, creating courses, and of course, giving talks just like this.

So today we're going to talk about reusable components. And the entire reason that we want to do this is so that we can write less code but get more done. We want to make our code more effective. We want to use our time more effectively. So we don't want to spend all of this time rewriting features and rewriting them over and over again. And so this talk is more focused on that aspect. The reusability of our code.

Some people often ask me about the difference between reusable components and clean components because I have another course called Clean Components Toolkit. And so they often ask what is the difference between these two things? And the difference is really about maintainability versus reusability. So clean components are more about long term maintainability, whereas reusable components are more about being more effective, being more efficient with our time, writing less code, and getting more done. Over the past few years, I have put together this framework for thinking about reusability so that we can better figure out exactly what amount of reusability we should add to our components. Because we don't want to add tons and tons of reusability to very simple one-off components. And we also want to make sure that we are adding the appropriate level of reusability to those components that we know we're going to use over and over and over again.

So the first level is templating. Then we have configuration, adaptability, inversion, which is perhaps my favorite one. We have extension. We also have nesting. So these first three levels are a little bit easier to understand and simpler. And they also don't give us as much power and flexibility as the later three. So we're going to focus our time on the last three, although we will go and cover these first three just so that we have the context because we need to build up into those last few levels of reusability.

So level one is templating. What we have here is that we want to reuse the different parts of our template. This is exactly what you're used to with building components and encapsulating different bits of UI and markup and logic inside of your components so that we can drop them in different places around our application and reuse them. This is pretty straightforward and not a lot to say about this one. Although of course there are a lot of nuances in how do we split these different components up? Where do we put the boundaries? Should we make a new component or not? And so we don't really have time to go into those nuances in depth in this talk, but those are there. The next level is configuration. And in this level we're going to create different variations of a single component, different variations in the behavior of that component.

2. Props: Configuration and Data

Short description:

We'll use props to achieve reusability and flexibility. Configuration props allow us to toggle behaviors and configure components. They can be boolean or enum types with multiple acceptable values. Data or state props involve passing application data, which can vary between users and runs of the application.

We're going to do this by using props. And with this there's a distinction between the three different kinds of props that we've got. So we have configuration props, we have data or state props, we also have template props. Configuration props, these are the star of the show here, as you can probably guess by the name. These configuration props are what drives this level of reusability. They are what allow us to toggle between different kinds of behavior and get a lot more reusability and flexibility out of each component.

So typically these are of a boolean type where we're either toggling something on or off. And good examples of this might be an is primary on a button. So you can have a primary button, you could have a secondary button. As well as things that are going to change like the UI of a component. So you might want to have a more compact view. Maybe you've got a video player and you want to show controls or hide the player controls. Different things like that. These are configuration props, because they allow us to configure and to change that behavior. The other kind of configuration prop are enum types or enumeration types. So these are where you have a list of acceptable values. It's not just a true or false toggle, but we instead have any number of values that we can pass into this. So one example is that the software I'm using to present this slideshow is Slidev. And it comes with a component built in to display a table of contents. And that has a prop called mode, which has three different values that you can pass to it. No other values are valid. So it can be all only current tree or only siblings. So that's a good example of a configuration prop.

Next up are data or state props. And this is probably exactly what you think it is. So we're taking our application data. Usually we're grabbing it from somewhere else, maybe an API, maybe your own API. Maybe it's entered in by the user. And we're passing that data around. This data is usually different when different users access this application or on different runs of the application.