YouTubevideo· May 31, 2024· 11:31· 4,300 views on YouTube

Getting Rid of JavaScript Completely is (sometimes) a BAD idea

Watch on YouTube ↗
0:00

React at a moment you like. The best moments light up here.

transcript

26 moments
  1. [Music] okay I didn't want to have to make this video but it flows into exactly the type of video that I've been wanting to make for a while and while before you start writing in the comments I'm not actually going to start bashing rails or hot wire or anything else that's akin to this

  2. type of format because it's something that I love within LEL I love live fire which is basically lel's version of HTM X where you're basic able to call server functions from the client without having to make those API connections by yourself magic happens where you're being able to call those methods those functions from the client but you do have to wait for the server to give you back HTML but the reason why I want to make

  3. this video and the reason why I've had it in my list of videos to make for a while is when do you actually call server methods and when should you actually just use [Music] JavaScript so we're incredibly blessed in lville ecosystem because the creator of Live Wire Caleb porio is also the creator of Alpine JS and they are so closely knit that there's a lot that you

  4. don't expect and don't understand actually happens within Livewire because it's actually using Alpine J s behind the scenes but there's still ways that you have to think about how to write code when it comes to writing a delayable application with live wire and Alpine JS because not everything needs to grab and retrieve information from the server sometimes

  5. that information can be retrieved behind the scenes or maybe loaded on the first page load and then displayed to the user so here's my video on when to use live wire and when to use Alpine JS okay so this is a very gross oversimplification of what the hay app is trying to do but I want to emphasize in state when should you actually use methods Livewire

  6. methods Livewire calls functions that are being run on the server versus when should you use something like Alpine again you don't have to use Alpine Alpine Works incredibly well within live wire but this is a simple way of displaying when would you want to actually wait for the server to return something versus when would you just use JavaScript on the front end well let's

  7. take a look so here we have a Livewire volt component this is just to show all the logic and view on in one file so I'm not converting back and forth but we have a button and we're saying hey we want to show a form now this could imagine this being a popup or imagine this being maybe a modal or a drop down something where you are wanting to display and then run server side

  8. information but you aren't displaying that on page load so how can we do this well in Livewire you could have a state that says hey display this form uh if it's if it's true right here we're saying if this display form variable is true then we're showing this so we're not actually rendering this until this variable is true well within Livewire this works perfectly fine um if I was to click show

  9. form we're showing the form and then we're we're moving it of course this is what is happening behind the scenes like in our Network request um if I also to click show form we're sending a fet request we're getting the information back to the HTML to actually say hey show me this form give me that HTML and now it's being rendered for example if I was to refresh this on this page you would be able to see there's there's nothing else it's just this show form information of course there's a bunch of

  10. wire magic that's happening right here but you can see these if block and end blocks see if I can zoom in if block and end blocks is happening right here that's basically saying that's where those at if and at and if blocks are within our blade syntax we're not actually sending that HTML to the page right now until we click show form so if I was to slow down down the network you

  11. would imagine what actually happens so if I was to slow down the network we'll say slow 3G we'll go ahead and refresh just so that we know it's all happening in slow 3G okay finally refreshed click show form nothing happens nothing happens and then it loads now of course there are ways to make this a little bit easier on

  12. the users and I already have a video going over how to use op mytic UI within Lille Live Wire because we have wire colon uh loading directives where we can say when we're actually waiting for this to load return back from the server because we're saying show form on the server show me a loading spinner or better yet just show the form until we're ready to go well instead of having

  13. to do that why not just use Alpine JS so let's take a look okay so how would this look like if we were to refractor this using only Alpine JS now we would take you we would take the display form State we'll move that to Alpine JS you can put it in this x data and then of course this can be done using any other client side minimal JavaScript library but Alpine works great because it's just

  14. kind of embedded into Live Wire I'll show you why I chose Alpine specifically because of one small factor that I think is helpful to know for for these full stack you know server side versus client side interactions so we have this x data display form is false we're saying anytime we click this button uh we'll change the display form to either false or true and then we can even show

  15. specific text of when it's not it's hidden we show it when it's not hidden we say hide form and then we can still call these wire submits within this form now one of the reasons why a lot of companies and a lot of products and probably hey does the the server side interaction where you're saying I want to have this Callie server method is because maybe you're wanting to check is

  16. this person even able to open this model is this person able to see this form or maybe when they land on this page if a specific requirements happening within this person's uh user or their account maybe I want to show them the form dynamically I saying hey certain requirements happen or maybe they hit a certain page that I want to automatically show the form well we can do that we'll go ahead and see what this

  17. looks like first but that is completely possible within Alpine and within Live Wire so show form hide form now if we were to you know set this network to even smaller looks like it's a fast 3G we'll just set a slow just a show you know once that initial page does load still loading still loading still loading okay it's loaded then all of this is just client side JavaScript

  18. there's no network requests happening but like I said maybe there is that need for when the page loads you're kind of checking something first to see okay should this variable be true or should this variable be false again gross oversimplification of what that UI might look like but there might come a time where you want to show a model by default if a specific variable is true

  19. or false on the server or in that user's account well Live Wire and Alpine make this incredibly simple there's this functionality called entangle and what you're saying is I want to have this display form property be linked to a state property within my Live Wire component so maybe display form shouldn't be false all the time and just

  20. for simplification I'll just say display form is is true and right here we could you know in our our our um our width statement this is basically saying when the form is rendered I want to or we could even do in our Mount this is saying when the form or when this component is mounted I want to set display form to True based on some variables of course you can do that but

  21. we'll just say it's true how do we get this changed well we can do something like this display form is wire. inang Le and then we can just say we want to entangle the display form variable Now by default what this is doing you can actually have wire. entangle dolive but Default about what this is doing is it's going to entangle this when the component is loaded when it's registered

  22. but until you actually want to make a network request in this case until the form actually submits it's just going to default to let's go ahead and refresh again we're in slow 3G Pages loading loading loading loading loading and there we go page is loaded slow 3G the the form is open because we set that in the server hide

  23. form show form no network request isn't that great again there are incredible number of ways to kind of display this and even real world real world scenarios where you might want to run things on the server versus run things on the client but my philosophy about this when it comes to using Livewire when it comes to Alpine JS is you use whatever you need that information from if it's something

  24. that does to interact with the server well then use this server you don't want to use that on the client and then wait until they press a save button or something like that to actually save the data there's a reason why we have wire loading directives to help that maybe slow lowness of a network seem a lot better seem a lot faster again I have a complete video talking about wire optimistic and basically how you can

  25. have optimistic UI in Livewire but if you don't necessarily need something to run on the server in this case showing and hiding a form or a modal or a drop down most of the time that variable shouldn't be decided by the server most of the time you're not going to need to run some T some sort of Auto uh authorization or any kind of database query before you actually display that

  26. modal or display that form to the user so in this case just let it be JavaScript for right now and then when you need to have that interactivity well Livewire and Alpine make an incredibly simple to blend those to and Tangle those together so use the server when you need to use the client when you need to too [Music]

just you here