So your Twitter newsfeed is going to be different every single time you look at it, although the configuration props used in those components are going to be the same. One useful question to ask yourself to gauge whether this is a state prop or not is if it could be stored in a database, because typically application data like this can be stored in a database. And so there I've got a few other examples.
We've got user, tweets, or posts, depending on what you like to call it, and movies. So if you've got an application that displays a list of movies, all of the movies fetch from your database. That's changing over time. And that is data props that you'd be passing in.
The third and final type of prop is template props. But for that one, we need to first cover and go into level three, which is adaptability. So just to quickly recap where we're at right now, we have these six levels of reusability. We've gone through level one, which is templating, and level two, which is configuration. And now we're on level three, which is adaptability. The whole point of this level is to use slots now to make our components more flexible. We're going to make them more flexible and adaptable to future use cases. And this allows us to not be constrained as much by props, which are very limited in what they allow, but to expand what can be done in a component by putting in slot.
So let me give you an example here. So here we've got a button, and this is an example of a template prop, because I did promise that we would cover this, and so we're going to cover it. This template prop example here shows a button, and we've got this text prop. Now this is a template prop, and the reason that it is a template prop is that it is only used in our template. We're not passing it to any components as props. We're not using it in a computed ref or in a watcher or any logic like that. We are just putting it into that template, and so that makes it a template prop.
The interesting thing with a template prop is that we can convert it directly one-to-one to a slot. And so what this allows us to do is that before we were only able to pass in text, a string, but now we can pass in an icon and some text to this button component. We can pass in whatever we want. So this makes our button component a lot more flexible because we are now treating HTML, and components as a first-class data type, whereas before we can't do that with just props. We're limited with props, and slots allow us to open that up. And the key thing here, why I call it adaptability, is that when you first create a component like this, you may not expect to add icons to your buttons. But now, because you have this slot, you don't have to go back and edit your component. We can just pop an icon into that slot, and away we go.
Comments