18. Refs


So we're now going to cover refs, which are a way to pinpoint an element within a component
so you can reference it, basically. A ref stands for reference. It's an incredibly simple concept, but to make this a little bit more fun, we're going to go ahead and create a file chooser, which is just a native HTML element.
And then when we choose an image, this renders a preview of the image. So this is really great for things like previewing an image before you upload it, specifically for avatars in this case. So over to the code, let's look at the basics of refs before we dive into that example.
Let's create out a simple component in here. And let's just create any element inside of here. It doesn't matter. It can be a div, an input.
Really, really doesn't matter. So I'm just going to put some text in here just so we can see something on the page. And inside of our xinit method or a method inside of xdata, we're going to go ahead and access this element and console log it out.
So let's go ahead and say console log. Now, we want to reference this item. Regardless of what it is, it doesn't matter. Now, to do that, we could say document.getElementById.
We could attach an ID to this if we wanted to. If you did want to do that and say greeting, you could easily do that by getElementById and greeting. And that would actually give you that out.
Let's give that a refresh. And there we go. We get the element. But this is a little bit cumbersome.
So instead what we can do, instead of assigning this an ID, we can say xref. Now, that's going to reference that by its name that you've given here. So now what we can do is instead say refs greeting like so. Let's go over, give that a refresh.
We get exactly the same thing. Now, refs contain any of the refs that you've created in here. So let's just go ahead and create another one in here and just change that to another. And if we console.log out refs, we get an object with each of them in there.
So we can have as many of these as we need. This can be really useful for not only the example that we're about to look at, but for an example, if you had an input that you wanted to be auto selected within a modal where the user doesn't interact with the modal before it's opened.
We looked at the example of modals earlier. You could assign a ref to this and just say input. And then when that modal gets opened, you could say refs input. And then focus, for example.
So when that modal gets opened, this automatically gets focused like so. I've just refreshed the page again. So you can use this for pretty much anything. But let's go ahead and look at that example of the image preview,
which I think is a really cool example to look at with Alpine. So we're going to go and create out a form here. And let's go ahead and add some data to this to initialize this component. Let's pull this down.
And inside of here, we want to store the image preview for whichever image we select. By default, that's just going to be null. We can create an input type of file inside of here. And that's going to be where we select that.
So we're going to need to watch for that changing. So we need to watch for this having a new image being selected or file in general being selected. And then do something with it. So for this, what we're going to do is attach an event handler.
We've not looked at this specific event yet. But again, it works in exactly the same as any JavaScript event. On change, which we use for files. And we're going to go ahead and call a preview image method.
Let's grab that and create that method just up in here. And let's just console log out changed just so we know that this is working. Let's go ahead and choose that file that we had before. And there we go.
We get changed. So what we can do now is take this image from here. And we can create a preview for it. To do that, we're going to use xref.
And we're going to define that as image. So we can easily access it directly from inside of here. We could, of course, use the event that we get through. We actually get an event through in here.
Let's just log on that event just so we can see what we mean. And if we just select that image again, we get the event through here. And we do have the target. You could do that.
But we're going to go ahead and roll with the xref in this example. In fact, let's bring that event back in. Because I think it's going to be a bit easier to do it in this way. Let's go ahead and create out a reader, which is a new file reader instance within JavaScript.
And we're going to go ahead and set an onload event for when that gets loaded or something gets loaded into it. So we have an event through inside of here. And we're going to say image preview.
And we're going to assign that to the result from that event. In fact, we don't need this event just here. We can use the one that we get back from onload. So now that we have our event listener, let's go ahead and say reader read as data URL.
I think it's capital RL. We'll find out in a minute. And then we're going to go ahead and from our refs, grab the image ref, which is this input. And then go ahead and grab from the files the first file.
Because we're just allowing one file to be uploaded. So if we just go ahead and console log this. And I'll run through this in a moment if it doesn't make sense. And get rid of all of this stuff.
Let's just see what that gives us. Let's choose a file here. And there we go. So we've got that file inside of there, which we can render out as a preview using the file reader.
If you're not familiar, what this will do is it will create a file reader. Ignore onload for now. It will go ahead and read that file as a data URL. And when that happens, this is our callback.
So this callback here will be run. It will set the image preview that we have up here to the result from that image preview. So what we should now have, if we just output a span with just x text for now as the image preview, we should get that result back in there.
It's not going to look great at the moment, but we can figure that out. There we go. So we get the image here as a string, which we can put directly into an image tag. So with this, what we can now do is use a template.
We can use an xif. Remember, we don't want to actually render anything out yet, if that's the case. So we're going to use xif rather than xshow, and we're going to say image preview. So if the image preview exists, then inside of here, we can just create out some kind
of wrapper, have an image in here, and we can bind in the source. So we can use xbind, bind that in to the image preview, which is that data URL that we got back. And of course, you'd probably want an alt tag in there for accessibility.
Let's just add a inline style to this, since we're not using much CSS here, and just set that to a width of 200 pixels, and we should be good. So now let's go over and preview this. Choose a file.
That's going to go ahead and read that data in, and I'm just going to give that a hard refresh and try again. Let's choose that file. And there we go.
So we get a preview of our image, which we can then go ahead and submit through and do whatever else we need to. So this could technically exist as a normal form that you're not sending through necessarily as an API request, but you're sending through on a normal page.
So all we're doing is sprinkling in this functionality to preview the image before the user goes ahead and submits the form in the normal way. For example, you would probably have a method in here of post, and you might have an action going through to some kind of profile information URL.
So that would actually submit that file through as normal because you have the input in there, but we've just sprinkled Alpine in to preview the image before that gets sent over. So there we go. A quick example of how we can use refs to reference any element.
That's pretty much it for refs. That's all it does. Pinpoint what you want this to be called, and you can access it using this refs object anywhere inside of your Alpine component.
19 episodes2 hrs 10 mins


Alpine.js is a refreshingly minimal JavaScript framework that gives you the reactive nature of Vue and React, but with much more simplicity. This course will get you up to speed on how to use it, with plenty of practical examples along the way.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!


No comments, yet. Be the first to leave a comment.