YouTubevideo· Mar 7, 2025· 27:08· 11K views on YouTube
React at a moment you like. The best moments light up here.
it's getting harder and harder to write bad Livewire code usually if you're writing Live Wire code you have to be consciously thinking okay what kind of code is actually being rendered is being run on the server and then what kind of code is actually being run on the client when am I writing client side code when am I writing server side code and when should I be writing client side code versus server side code in this case when should I be using Alpine JS versus
Livewire but the new update to Livewire Livewire V 3.6.0 Caleb porio has kind of invented I guess a new way to write JavaScript within Livewire and I actually haven't gotten the chance to check it out just yet so that's why I'm making this video to be able to say okay here's all the new things that are in Livewire
3.6.0 but here's all the things that make it that much easier to write just better Live Wire codes so why don't we do that together I think the way to write Live Wire has changed but before we dive into that why don't we check out our sponsor for this video I'm always trying to find a way to learn new skills or programming languages without taking up too much of my time Cody found at cody. te. COD dy.
Tech it's kind of like a du lingo for programming where it's it's engaging it's fun they're bite-sized lessons super quick to do and I can even take on the go like on my phone for example there's it's made up of Journeys and challenges where if I want to learn C or rust for example it kind of takes you step by step the platform pretty much has everything you need to complete the lessons right there in your browser and I love that each lesson is kind of text spased and then has a challenge that you need to complete and the good thing is everything is really quick like I said I
use it on my phone before I go to bed so that way I can just learn something new in this case I'm learning C and rust and the neat thing is everything is gamifying so I have an incentive to keep coming back to the platform but really it just feels good to be able to check something off my list especially when I'm laying in bed at night and wanting to keep my brain uh fresh cody.
Tech is the first thing that I've been turning to you can find it for yourself and check it out at cody. that's Cod dy. te I've been kind of busy with the latest releases from the Lille team with the launch day happening last Monday which
means I didn't get the chance to really play around with all of these new features that came out with Livewire 3.6 and there are a bunch of them so why don't we dive into them together and see which ones are going to change how we write Live Wire and more importantly how is it going to change how we think about how to write Live Wire in this case I think a lot of these things really just have the ability to help you stop thinking of Livewire and just server side interaction and more about okay
it's all of the features all of of the code that you need to have beautiful client side pieces as well as server side interaction so JS uh this JS I think that kind of already existed but I could be wrong in terms of what might have changed there wire text wire show wire transition wire cloak these look familiar if you're used to Alpine JS because there's X text X show X transition and X cloak but now it's just
live wire so it's not Alpine JS anymore so what does that mean mean for us am I a professional Alpine code person now I don't know U but yeah let's go ahead and I'm going to read the lville news article to kind of walk through all this but I'm going to link Caleb's tweet in the description below as well but in the
larel news article 3.6 release some JavaScript actions added support for defining JavaScript actions which are JavaScript functions that can be called from wire click or wire either run client side code only or to apply optimistic updates before triggering a server request this is the the this is the cool thing he actually put out a uh kind of blog an email newsletter before creating this Livewire 3.6 update saying
hey this is the new way to write live wire a script block underneath for any kind of client side stuff and then you have all this up on top and it kind of evolved in a really short time uh he's a wizard into this JS do something but then you just have wire click do something so you're calling JS and not a
wire method the first thing I see here is I'm curious so we'll play around with this right now but I'm curious if if I had a uh public function do something and a wire click do something which one would it call would it default to client side first and then this let's actually let's see um so I have this right here
let's just say we have um a I think I have flux here because this the new uh we'll just say wi do something I just want this to do something all right and so then here I want a script oops and I want and script and in this script
I want the ability to do something why don't I just actually instead of typing this all out why don't I actually just copy the code that that would probably make it a little bit easier I'm just going to copy all this uh we'll just copy all this right here there we go I already I already wrote the rest out we just copy this right there perfect JS do something but if if I had a public function do something up here which happens first do
something or like the actual console.log um let's see got call composer Dev I need to register because I put this on the dashboard all right do something click okay DD first did it actually uh console log let's see
console okay okay so it looks like server first it's probably like if I was to let's go to the uh to the docs uh I'll open I'll open something the docks up in a new tab we'll say JavaScript I'm pretty sure this is where it is uh JS increment okay but in the docs he has this dollar sign JS dot but in the uh at
least in the Live Wire news thing it looks like I can just call wire click on this let's see what the um let's see what this one my favorite one is JS uh okay so I think this is the correct way of doing it the lville news
might be incorrect right here or maybe you know maybe I'm doing something wrong let me go ahead and let's try it out so if I was to remove this public function do something do we still have that automatically being called do something no does not exist okay so it needs to be js. do something okay so now we have
refresh bam do something in the console right there bump that up I still think this is super neat because when you are again when you're thinking about how am I writing better Live Wire code most of the time people who complain about hey Live Wire is not good at X Y or Z most of the time it's because you're you're thinking of Livewire as a sole server functionality
sole server interaction instead of thinking of it as I only need to go to the server when I actually need to go to the server but now there's these Primitives to say okay maybe I do need to go to the server eventually but it'd be nice to do something on the client first instead of waiting for a return from the server and you could do don't get me wrong all all of this stuff that we see here in Live Wire 3.6 release all
of this stuff you could have done previously it was just kind of you would do it within uh Alpine and then you would call Live Wire within an Alpine function now this just makes it and I could be wrong on this again but I'm pretty sure all of this is just a a neat wrapper to make it feel like you're not writing JavaScript you're writing these neat wrapped primitive and being able to do things on the client so if for something I want to now
I can call uh wire dot was it wire call let's see wire I just called the actual method so if we say um public function from server uh DD high from server uh so if I
should be able to I always forget what it is in JavaScript anymore it's not sleep is it sleep can you sleep in Javas script it's not sleep is it wait wait th000 and then we should be able to
call wire Dot from server is it a is it an is it a function or is it just a method okay it's a function so that should be correct refresh do something weight is not defined is it not weight all right it's late and I wasn't fully thinking of of the actual JavaScript method to be able to do this it is set timeout not sleep I don't know why I was thinking I've been writing PHP too long now uh set timeout so if we do you know
wire from server we essentially can do things like being able to optimistically set something so what we could do I believe is we could say um let's just say during this time we have a specific timeout uh we want to well first we want to uh wire and this is where we can I think we can change the text of something without making a round trip we could say um wire
so if we have a public string of messages hello world I think what we could do let's say we have a span with wire text equals message
then here we could say wire. message equals hello from JavaScript and then call the from server method what this should be doing is we shouldn't necessarily hit a round trip to the server this wire. message changing the wire text should all happen client side I believe if I'm understanding everything correctly so do something okay hello from JavaScript and then hide from server okay so let's check take a look at the Network so I'm going to clear this out
refresh clear it out just to be safe okay do something yeah no network request hello from JavaScript hi from server after 1,000 milliseconds oh man this stuff is so neat because now we get to do I wonder
what what am I getting here oh it's because I I did I I di died and dumped that sounds so weird if I just say I did or you know the things we do as PHP developers but the thing I really love about this specifically the wir text with the JS call is now I'm I'm going to even think about okay what how would you do this previously well um you would say like
xon or just like you would do like the the Alpine version of a of a click instead of wire click and then you would actually have to write out the uh function here within Alpine data but and then the text is even crazier because then I would have to make sure that the text is set and then maybe I want to entangle it because then I'm curious if H that's very interesting I'm curious
of like if this changes it on the server then if if I'm updating something if that does that make sense if if I change something here and then I want to um you know die and dump the te text
the updated text on the server so if I was to die and dump uh this message is it going to be the updated message is this bundling it and then updating it on the server so you could do things like you know wire text for forms possibly
where you could optimistically update something you could change let's say you have the the wire Tex text of a to-do and when you click to add a to-do you could have that wire text actually populate but still be saved to be sent to the server I think this should work and it's crazy that it does so I have Hello World should is the message that's going to be um dded going to be hello world or
from JavaScript do something hello from JavaScript are you kidding me that's crazy this just makes this kind of stuff so easy because you you don't have to worry anymore about okay how am I keeping things in sync on the server and the client um you don't have to worry anymore about how am I going to you know make sure that I'm not leaking anything
on the client or just keeping something on the client and then doing some weird entangling stuff I'm pretty sure this kind of negates having to ever do why.
entangle ever again because it most of the time when you're doing something like that in the past let me kind of pull it up and um I believe Caleb had already kind of said that uh this was kind of not needed now with wire but it was still needed in some specific cases because you wanted to keep values in sync between Alpine client and server Live Wire but here you have uh okay um yeah all of this is is
not necessary anymore one we now have wire show we haven't gotten to that yet but now we have new HTML directives we already kind of touched on wire text but now we also have wire show to be able to say I want to change a something on the uh client without having to send a server request uh this is this is crazy
we have X on click I think that's still necessary NE I think that's still necessary so now we have the wire show show model so we still need the X on click we can't do a we can't do a wire click I don't believe um let's see how what the what the Tweet says as well for wire show can call JS
methods from the server um it might not be set up here let's go ahead and look at the documentation we have show wire show okay basic usage Chicago create post modal yeah so X onclick and so what
you would have to do previously would just have this data live in Alpine but why wouldn't you want that why would you only want why would why would you want something on the server well um let's say like this is a great
example of it if you want to save something but then close the modal because that state lives on the server and now you're sending a server request and then sending it back before you would have to uh you know change it on the client and then send the server request it was it was it was complicated
not not terribly complicated but it was complicated enough where you were like hey doing something like this means I just have to only do an Alpine and then you know send something send a JavaScript back and I believe the uh this JS let's see the Js
um JavaScript using JavaScript and Live Wire components I believe yeah I believe that this JS already existed and I could be wrong on that I have a video kind of talking about it that I'll I'll put up there in the corner but I believe this kind of already existed but now it just makes a lot more sense because before it
was like okay what are you actually going to do here but now it's okay I'm already sending stuff from the client to the server and keeping it in sync and Tangled and now it just makes it easier so for stuff like this wire show so if I was to I'm just going to grab all this and then we'll go ahead and cop paste this in here so I don't even have to use any script tag here that's crazy I like that it's cool that I can just write js.
something I like that like it's just a it's just a function I could technically make this can I make it async that's interesting probably um but yeah uh let's so if we have a public um let's do
public show model equals false and then we have Save which basically like it does something and then just says this show model equals false we could even say like uh this JS and we can say um we can say
console.log saving there we go is it is it a is a string okay yeah so Caleb even shows the async await where you can have celebrate async and then you're going to um await
wire. log celebration that's pretty crazy too so like you're you're you're doing all this asynchronously sending it to the server and doing with the optimization stuff that you need that this this stuff is so good you people who are just getting into Live Wire don't know how easy you have it right now this is this is awesome stuff does he mention this JS any anymore okay yeah so this JS uh so you can call specific
JavaScript functions from this JS I think that would probably be the new thing now before you could call um console. logs and stuff like that but now you can actually just call JavaScript uh functions itself so we could say like this JS um I don't know do something and
then this could just be a console.log doing something okay so this should theoretically work we'll pull open uh our browser here okay new post the Modo comes up um
save post closed it doing something okay great it's hard to see because I don't have it as a actual button but we'll do like flux button just to show flux button perfect and then this will be flux text area I think that's a thing flux text area blocks button blocks button turn new post hi there save post
closes the modal it says doing something in the JavaScript and then we should technically only have uh one network request so we we'll clear the network new post should not make a network request see doesn't make a network request and we probably could even say
um wire. show Mo modal equals like the opposite of wire show modal that should be a thing does that work
yeah that's sick no network requests so clearing no network requests so essentially you're writing Alpine but without having to have the data so previously you probably would have had to do x- data or you could technically entangle this element and
then have it be like that again I I kind of go over a lot of this in previous videos because it was a little bit harder to to kind of grock to understand say okay what should I be doing on the client what should I be doing on the server and now it's just easy this xdh on click is saying I am doing a client
side click but I'm setting state from the server absolutely crazy um where now you have like the wire click to say I want to call something JavaScript and then you automatically know this is Javascript it's as simple as that okay what's new I'm I'm getting I'm getting too tight of the we it's going to be a longer video than I expected to have okay we have the new HTML directives wire show wire text wire
transition is that is is wire transition new or is it just like kind of wrapping so transition uh showing your hiding content Live Wire is simple okay wire transition um so we have if show contents button wire click set show contents to True uh um so yeah let's let's let's take a look at this so if we have this div
here and then we have a public uh we won't even show the post we just have show comments so we're just saying div wire transition and we'll just say comments here perfect so if I take a look here we're doing is we want to show comments click show comments are we
making a network request I believe so we probably have to be right yeah so like this is enabling something but now we have transitions for these requests this was a thing before was it
not wire transition I could be wrong let's see does Caleb say it's a new why transitioning what am I remembering wrong I I thought the wire transition was a thing but maybe maybe it was a thing and then it was kind of taken away and then it came back if you know let me know in the comments because now I'm second guessing myself as to uh what I
actually thought was already existing in Livewire I know wire cloak is X cloak I would imagine but for Live Wire but I thought wire transition was already a thing like wire I thought I had done this in some videos I couldn't again I might be losing my mind I don't know um yeah so we have default you can customize the transition style I thought this was thought this already existed but maybe it exists in a different state now okay and then finally we have uh
wire text wire cloak let's take a look at wire cloak an directive that Hinds elements on page load until Live Wire is fully initialized uh flash of unstyled content okay very interesting because we already kind of looked at these these wire show directives so let's say um you're doing something on the client side to show or hide these particular elements wire cloak very similar if you have ever used X cloak directive uh it it's great to say okay I don't want to even show this
until I know for sure that it should be shown uh this is not as common so I'm not probably going to use this one as much as I am going to use like the JS and this JS I think that's really the one that I'm going to be using the most
is being able to have a button and on that button immediately call something from JavaScript to say hey js. do something this is just incredible to me
to be able to now write JavaScript more cleanly in line Live Wire to be able to say okay now I have this uh you know button and in my console to log we should be able to see we're doing something we're not sending a network request but now I have the ability to call serers side functions even a synchronously from this JavaScript
function without having to do all this magic of making sure it's connected to the button and then you know actually like creating an a specific JavaScript function for it it just makes it feel a little bit more like Live Wire and
that's really what I think all these features come down to at the end of the day all of them again I could be wrong in assuming this but all of them could have been done previously but they weren't necessarily easy to do in a lot of ways and I think this just gives you the ability to write JavaScript and Livewire and server and client side functionality a lot easier not having to or are blurring the lines more in order so that you cannot mess
things up even more so let me know what is your favorite new feature within Live Wire 3.6 is there something I missed is there something I'm misunderstanding especially maybe within that wire transition but overall I'm excited for the future of Live Wire I'm excited for the future of Lille and excited for the future of JavaScript developers and PHP
developers who are realizing there's some cool things to learn from each side of the aisle so whether you're writing Live Wire whether you're writing view react keep building keep creating