YouTubevideo· Sep 3, 2024· 6:33· 5,300 views on YouTube
React at a moment you like. The best moments light up here.
Here's a super quick video I wanted to record talking about one, actually two of my favorite features in LiveWire, specifically LiveWire v3 that enable you to create more SPA kind of functionality within your LiveWire application. Now, I'm talking about this because I tweeted about creating this within LiveWire.
Having search functionality and storing that to the URL, and then having these, you know, almost nested layout type features where you have books and when you click on a book, all of the tags and search and state stays the same in the outer layers of that layout. And yes, LiveWire might not be the exact perfect use case for something like this, but it's still completely possible. And while I was building this out, again, I had to put this on hold because of Laracon.
So I'm not completely finished with it. I do want to make sure it highlights a lot of the, my favorite features and just the best features of LiveWire before I put that out into the open. But here's a snippet, if you will, of the two features that are going to enable you to build something like this SBA kind of functionality without having to use something like reactor view with inertia. Again, reactive view with inertia, spelt with inertia are going to be fantastic options to create something like this.
And Laravel still gives you that, that backend to have all of that pagination and search without having to implement it yourself. But it's still doable in LiveWire. The first one is LiveWire Navigate. So just a quick glimmer or a quick glimpse into LiveWire Navigate is you have any route, whether you put it into your web routes or you're using some kind of like full page components, like I'm going to show you LiveWire Navigate attribute.
And then it enables like SBA fun, like functionality. When a user clicks on a link, LiveWire prevents the browser from visiting the new page. Instead, it requests the page in the background, shows a loading bar at the top of the page, and then places the information into that page. LiveWire replaces the current page's URL, title, tag, and body contents with the elements from the new page. And this works great with full page components. So let's say I have two routes and these are just full page volt components.
One is a navigate demo and we can show that here. Just saying Hey, this is the first page. no state, no anything on the page. And the second is this second component here, just like with the link back, both of them has this wire and navigate pre pended onto it. And both of those have these routes listed here. So if I was to show what that looked like, all of a sudden we have the first page. You can see there's no refresh.
When I click the button, it takes you to the new page, takes you back. Wire navigate is intercepting that click. And allowing you to visit the page, a cool additional feature on top of this is you can actually append the hover. If you want it to start prefetching the page on hover, instead of the mouse down, click again, it has this neat functionality where it starts navigating to the next page on the mouse down.
Modifier instead of the click modifier, just a hair faster, but you can definitely use hover. If you know that it's not going to be used on all of your links and someone's actually going to click on that link. We won't, we wouldn't use that for this example. For example, you wouldn't want a hover for every single book. You wouldn't want to prefetch that information until it's actually clicked on. That's one functionality.
The second piece of the puzzle in order to get enable something like this to work is the persist feature In Livewire. We have this livewire Navigate. There's a bunch of cool little snippets in here like Navigate Hover, but it's right here. Persisting elements across page visits. This is what kind of gives you that M SPA like functionality.
For example, there might be specific elements on a page that you wanted to leave in the layout and not reload when you navigate to a new page. In this example, it's showing an audio player because you don't want that to restart every time you load a page. If you have like a podcast player, but you can take this functionality and make it. Even more in depth or more intricate if you want to. So I have this.
What if I had a counter here that had some state on the bottom? Good thing I already have that kind of whipped up. I can just have this persist here. With a live wire counter volt component, if I was to look at the counter volt component, it's just a simple counter, but we're storing that in the URL state. And then this is what that looks like.
Now we have a component that has the state on this component, but we're persisting that component when we go to a new route. Now you'll see this state isn't actually. Oops, need to refresh. Now you'll see the state isn't actually maintained or persisted with the component because that state is something different. So that's the next piece of the puzzle.
When you're building out something like this, one of the core things was I'm clicking into a book and then I'm clicking back into the layout. The state still stays on the side, but with Livewire, because we have the ability to put it into the URL, it makes stuff like that. Easier. We just have to make sure we grab it when, or grab it on mount when we go to a new component and things like that become relatively simple when it comes to live wire.
Again, I'm not saying that live wire is the perfect or the only way to build something like this within the Lyrabel ecosystem. There's reasons that view and react and inertia and spelt and anything in between exists. Or if you just wanted to build it with blade. It'd be doable, but you'd have to use like some Alpine or some other JavaScript on the page. But I did want to talk about these two features. Livewire navigates and then persists. That would be the backbone for how we build this out.
So stay tuned for the full video as I kind of build out that full application because it's going to be crazy. It's going to be awesome, but there's going to be some really good snippets that we can learn from.
So keep creating.