YouTubevideo· Jul 26, 2024· 9:12· 3,200 views on YouTube
React at a moment you like. The best moments light up here.
I've been writing a lot of JavaScript recently it's Alpine JS and it's an aaral application so it's not too crazy but I've always admired that live wire especially in version V3 Live Wire and Alpine JS are so connected that it just works with simple things that you might want to do on the server or on the client or maybe from the client to the server or the server to the client and here's one thing that you might not know you can actually call JavaScript from
PHP this is a pretty standard Library application where we have a list of users that are paginated and we can search up here so that we can return that users live using this search now the browser does have some nice features if you are using the type of search in the input where you can just clear this and it resets everything without having to do any of the things that we're going to be doing here in a little bit let's imagine for some reason that you need to clear this input field or maybe like clear specific variable form whatever
you might have without actually omitting the form well most of the times if you're thinking in live y terms you're like okay I'm going to write a function a method on the server in this case maybe like a reset query function where I set that state to blank and yeah you
would be correct you could do exactly that reset everything works just fine but that function that method is running on the server if we were to open this up and head into Network maybe switch this to slow 4 G reset okay the page load um
Dalton let's see uh we'll say Alfreda there we go reset and it takes a couple of maybe like a second to reset that thing so it's it's not the worst UI or ux in the word world but most of the time you're probably thinking JavaScript is going to be the best answer for this I just need to clear this so that the component can reset I don't really care if it takes a
little bit for the component to reset in this case the search to reset that's server side I would expect that to happen a little bit but if I was to click reset I would expect that to at least clear pretty quickly so what can we do with this well because Live Wire and Alpine are so interconnected there's a lot of great methods and Magic that
Alpine has that we can use to interact with everything with up here within live wire for example in this button instead of wire click which is hitting this query right here what we could do is say x- on and that's going to say this is just client side click equals and now we
can call these wire methods one of the things that to note here I'll put this in the description below is there's these uh actions there's these magic actions within Livewire where you can say things like like I want to talk to the parent component uh and this is just live wire but I can set specific um variables from
Live Wire I can refresh the component from Livewire and with doing all of this without saying or creating own functions and methods within Livewire class instead of you just do it all inline so we could do that but it's essentially doing exactly what we have here we have this wire click set query so let's take a look wire click set query we're essentially just doing what this reset query is doing but just in line here
again incredibly useful not really useful for our specific purpose in this video we we do want to use JavaScript and Alpine is the the dollar sign wire is still passed down to all of Alpine
components so we have access to pretty much everything here within Alpine as well if we wanted to toggle a variable if we wanted to refresh the component we want to set the component or just get the component State we can do all that but we can also call specific methods as well so what you could do instead of this is say well I want to say X on click equals and then we can say dollar sign wire. query and that's that's that variable that we have dollar sign wire.
query and then we could say equals and set it to Blink so this is running on the client first and then hitting it's basically running on the client getting the value for this query State and then setting it
but it's not pushing that to the server we can take a look so refresh we have cie and reset okay nothing happens because we're just doing this on the client to refresh the page we take a look and we say to the network because if if we hit
this search C we have a bunch of different update requests that are happening within Livewire usually if we were to hit that reset button we would expect I want to hit the server reset the field and reload the component because the component reloads anytime we run a
function or a method within that component but clearing all this out hitting reset okay nothing happens on the server it's all client side well this is great how do we how do we reset or refresh our component well instead of doing it all in line which you could do we could say something like and then we want to wire.
uh refresh the component that's all well and good but you can write all of this in PHP little crazy I know let's switch this back to uh wire click so wire click
refres or reset query now instead of this query we can actually call JavaScript right here in the component so we can set this as an attribute to say uh JS we bring that in use Livewire
attributes we're just saying Livewire is going to basically initialize this as a JavaScript attribute so when we call reset query with wire click we're not actually hitting the server we're running it on JavaScript in this case our JavaScript is running wire query to clear this and then refreshing the component let's give it a Look Natalie
and reset there we go so it clears on the client first and then it runs that refresh so this is a great way to run JavaScript in your Live Wire without having to do all this fancy stuff of okay I want the button to be Alpine but then I also want to refresh the component in Live Wire you can do that this is just a helper method to say I want to run JavaScript in the component in this specific function that I'm creating additionally if you didn't want to do any of this let's say uh we just
we did want to say this query equals blank but for some reason and this is not the greatest example but for some reason I then want to run something in JavaScript after this button is clicked I did want to do all this on the server maybe this is a form method maybe this is you know like a button to notify someone but what if I want to send some JavaScript after that maybe we want to just do a Alert in the browser say hey
here you go there's other ways of doing this of course within Alpine but Livewire gives you this helper function of this JS we now anything within this uh parameter here is now going to be run on the client if I was to do uh maybe like let's say alert and we can say query reset let's take a look so Cali reset oh
there we go so we're running JavaScript in PHP it's pretty crazy but it's also kind of cool because now we don't have to worry about trying to uh okay I'm just going to set up my Alpine component or maybe I'm putting a script tag quick note you can say script here
and in a live wire component end script and now you can just write JavaScript right here but if you didn't want to do all of this setup and you really only needed like a few lines of JavaScript or a complete method to run JavaScript and
you didn't care to do it in line or use Alpine to do it again some fantastic magic methods that live wire gives I would advise checking out those docs to take a look at what is available but you really don't have to too you can just do it all within Live Wire all within PHP so the next time you think hey should I run this JavaScript maybe just run it in PHP