Next up we have props. Now, one of the ways by which we can pass data, maybe even modify behavior, is props. No matter, anywhere, like, whether we're using React, or whether we're using Solid, whether we're using Vue, it kind of works everywhere like that.
If we have a look at the props file over here, we'll see that it's very similar to the basic example, we give it a name, but we are passing some props down. So, this prop, myMessage, is kind of the default, let's say, and we reference it by props.myMessage, which is the input argument of the function that's the third argument of custom element.
We get to use this data if we don't overwrite it in the HTML, so let's have a look at how that actually looks like. Just pass some props without any props, and we're probably going to see another hello world, right. Not too exciting. However, if we do, like, something like this, we get to customize the message. Of course, hello from the other side. And if we pass it something else, this cursor being a bit smart, sure, why not. And we are getting something more.
Now, there's a caveat here. The only two types that we can reliably use here are strings, because attributes get passed as strings, or rather HTML supports string attributes, or booleans, having the presence or the absence of a certain thing. There are ways to pass other kinds of data, we can't really show that here, it's a bit more involved, but I'll try to do an example with that a bit later.
Now next up are slots. You're probably familiar with slots under a different name. In React land slots are sort of equivalent to React children. Now let's have a look how slots look like in this very weird world of solid-to-web-components compilation.
Now, we can use a slot tag. And slots come in two flavors in general, like named slots and default slots. Default slots are what you can just put in between the tags of your custom element and it appears in here, in this particular div. And if we use a named slot and pass it a slot attribute on the other side, it gets rendered inside this one. So let's see how that looks like in action. So if we use cslots and let's say we want to do Hello World, sure. Let's do this. Let's remove this one for now, we're not going to need it. And let's actually have a look at how this translates from the HTML over to the JSX we have on the other side. So we're registering this under cslots, sure, which returns a div with a bunch of slots here.
Comments