12. x-show vs x-if


We've been using X show a lot throughout the course but we also have access to X if which pretty much does the same thing but there's a slight difference. Let's take a look not only how this works now but also when you might want to reach for X if. Let's start with a simple example of X show as we have already seen a few times before
and let's set show to false within X data and let's create a div out here and say X show, show am I visible and of course the answer in this case is no. Now if we would set that to true we know that that's going to show it and of course if we introduced a button here to toggle this that would also work as well.
Let's do that now and then we can roll on to the next example of looking at X if. So we're going to say X on click show equals true and I'm going to say show me. Let's go over there we go pretty straightforward we've already seen this before. So let's copy this component that we've already got,
pull this down here and switch this over to use X if instead. So for this let's just pull this down below the button. Let's create a template around here because we're going to need to use a template for X if and we'll see why that is and let's change this to X if show
and then let's just output the div in here. Let's go over give that a refresh you can see it works in exactly the same way. So the question is why would we want to use or when would we want to use X if instead of X show? Well the short answer is if what you are doing inside of the condition,
so if the condition means that whatever you're doing is more intensive, for example you might be performing some kind of calculation once this is toggled then you would want to use X if. So this isn't actually run beforehand.
Now that might seem a little bit complex so let's take a look at a practical example of this. So I'm going to go ahead and get rid of this and let's come down duplicate this down here. So I'm going to go ahead and say show users. Let's just say inside of X data we had a list of users that we wanted to perhaps sort.
So I'm going to go and switch this over to users and we're going to have an array of people in here. Kind of like the example we looked at before where we had a name and then we had some points. So let's say that we brought back the example of ordering these users by their points. So let's say Billy and set that to 100.
We'll just leave it at 2 for now because the actual data itself doesn't matter too much. So let's go and say users ordered by points. And let's make this a getter and let's return this user's sort. Bring in our sort enclosure and then say b.points minus a.points.
That's going to order them by points in descending order. Now inside of here let's switch this over to an X show and say show. And then let's iterate through these. So say template and X for user in users and let's just make this really simple.
Let's just output the user's name in here. User name and let's have a look at this. Okay. We've got show is not defined so let's just bring that back again.
And we're going to change this to there we go. So what that has now done is when we click on it it's showing them users ordered by their points. Let's just actually bring the points in here as well. Change this to a span just so we get a good idea of this.
I promise there is a conclusion coming here in a minute. So let's say points and let's wrap this in a div real quick. And let's come over and there we go. We've got Billy with 100 points and Alex with 10.
So the point of this is if I go ahead now and switch this over to user's order by points, what that's actually doing is it's always running this. So we can prove that by console logging sort. Now this isn't very complex so there's nothing wrong with doing this initially on the page,
but that's always going to be run. You can see it's run here twice. Now the difference between x show and x if is if we were to create a template out in here and then ended that template down here and said x if show.
Now let's look at what happened when we initially land on the page. We only see this called once. We can get away with this not being called once by making this a method. So let's change that over to a pure method and there we go.
It's not called at all. Now when I click show users it's called. So let's keep it as a function rather than a getter because that's always going to be called. And there we go.
So if again we change this back to a div and then set x show to show, that's always going to be called because it's being rendered just not shown. But if it's x if that's conditional render. So it won't be called until we ask for it.
Hopefully that makes sense. It's a bit of a long-winded way of explaining that, but that's really the usage for x if. If you've got something happening inside somewhere that you want to only be run
like this ordering of the users by points, once the thing has been shown or the condition has been met, then that's what you want to use. That's the difference between x if and x show.
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.