Skip to main content
View on YouTube to comment and like!

Syntax Video

React vs Vue - Template Syntax and State Management

Chapter 1 0:00

Intro

I've been writing software for over 20 years at this point and for a large portion of that time I have been building front-end applications with many different things uh but I've built things with view I've built things with react and I've developed my preferences and opinions uh over all this time of building lots of different software um and there's been a lot of dialogue recently comparing View and react but in this video I just want to show the code I want to dive in and I want to compare some things that I have implemented in both react and in view now you might be wondering why should I care ultimately I just want you to see some real world examples because when you see things on Twitter very often it's a small image or maybe something in 280 characters or less and there's a lot more Nuance that can't be conveyed in 280 characters or less because we're all typically working on more complex applications and so in this video I want to dive into the code and really show you how things are done in both View and react so this video is going to be the first in a multi-part series where I show how I implemented the same application in both View and

Chapter 2 0:52

Example App Overviews

react and today's video is all about this signup form here now this is mainly demonstrating how we deal with State updates in a component and also how we can Define the template both in jsx and in views templating syntax uh but for instance we have field validation whenever a user tabs out it'll show is valid if it's valid we can submit the form and it'll validate the entire form so uh it's a fairly simple example but this is really going to demonstrate State updates and also be a comparison of template syntax in future videos I'm going to cover these other other examples so we also have a shop so we have a products page with two products we have a cart page and we need to be able to share State between these two pages and also be able to display the status of the cart here so if I add something to the cart you can see the status is displayed I can add multiple things that should update the quantity if I go to the cart page you can see that it has that data and I can also adjust the state of the cart from the cart page and I can also add other things to the cart as well so this is mainly a demonstration of how do we

share state in an application when we have multiple pages and multiple places in our app all need to be able to access that data and also modify it another example I have is an image search application so this lets you type in a search term this will hit an API give back a list of images and then we also have pagination so we can look at the next page and previous page of images uh and this example is mainly demonstrating how do we do asynchronous API requests inside of our components How We Do State updates and then also I'll show how I implemented the pagination here the last example I have here is a stocks example so this allows you to type in some stock symbols it then reaches out to the Yahoo finance API and shows realtime updates of stock price information but we can add multiple here and kind of just build out a little stock ticker dashboard you can see that it's actually validating the stock symbols before it adds them so that's reaching out to an API and really this is just demonstrating how do we deal with highly interactive and uh asynchronous applications right because

all of these charts are all receiving socket updates at different times times we need to be able to update the charts keep track of that state and we also have a fairly complex component hierarchy as well so this example is really going to show the differences between how you implement highly interactive client applications with both View and react but like I said we're going to focus on the sign up form for today if you have any suggestions for more complex client apps that you think would make for a good comparison between View and react let us know down in the comments but without further Ado let's jump into the code my name is CJ welcome to syntax [Music] now let's take a look at the sign up example now you may not think that this is that complex but when you're dealing with client side validation in form state things can get tricky um and for instance I want to be able to show a form error and show an invalid State on a field if a user tabs away I want to do the same thing and show that it's valid if a user tabs away and also show errors when the form is submitted so there's a lot to keep track of here now for both

Chapter 3 3:30

Sign Up Form Example

Chapter 4 3:55

Zod Schema Validation

of these examples I am using Zod so Zod is a scheme of validation Library basically I defined my form schema here and then I'm using this to validate the form I created a bunch of helper methods and like helper types that we can use but beyond Zod there's no other Library used in these examples now if we take a look at react let's see how we set up all of this state in react we have the use State hook and we can pass in an initial value here and then we have two things we have the state value itself and then we have a function we can call if we want to update that state value now I have form submitted which defaults to false but when you submit the the form we said it to true but this lets me know if I should show the errors or not because when the page loads you don't want to show the errors we also have the form field errors so basically for any field if there is an error message it will exist on this object but if there's not it won't so it defaults to the empty object because there are no errors when the page loads but as the user tabs through or submits the form this is going to get populated with all the error messages and the other thing is to keep track of whether or not a user has

Chapter 5 4:11

State in React

touched a field we have the form touch field so it has all the fields of the form but then this is either true if the user has touched the field or undefined if they haven't touched it at all so this also starts off as an empty object and then the form State itself is just an object with all of the fields from the form and their initial values so nothing too crazy here uh but let's take a look at how we Define the state inside of view so in view we have two different things we can use to define state one of them is ref now you technically can use this to store Dom element refs like you do in react but typically a ref is used to create a single reactive value often for Like A Primitive value like a Boolean here so form submitted is a ref that defaults to false and then we have the same form errors but here we're using reactive so reactive allows you to declare a reactive object but with reactive I can just modify this object directly so I could say like form errors. email equals email is required or whatever else and so reactive is pretty nice when you are dealing with an object so we're using reactive for both

Chapter 6 5:13

State in Vue

the errors of the form and then also whether or not those fields are touched and then the initial form State here now the major difference difference here is that we don't have a Setter function in view we are able to mutate our state so for any one of these values if I want to change it I can just write the code to change it so that's how our state is defined let's see how we access it in the render function so here in react we are going to look at the form here and we have lots of inputs right we have an input for email an input for first name an input for last name but for each one of these I'm binding the specific value right and that value is coming from form state so as the user types into each input field that's getting updated on State and we're binding it to each of these inputs in order to update that state we have the onchange Dom event so here whenever the email input changes we want to set that value on our state and for that we have a helper function here called set field and uh the way I've written this is with just some typescript types that basically gives us good uh type safety here so we pass in the specific property that we want to set and then we pass the value that

Chapter 7 6:07

React Render Function

we're going to be setting it to and so this is nice because I can reuse this for all of my inputs right I have it for first name for last name for username Etc but if we look at that function we can look and see how we actually update the state and we're using that set form State function right so this is the setter function that got passed back from UST State and for any given field that I pass in here and value we need to update it on the object but in react our state updates are immutable so we have to return a copy of the object so I'm taking the current form State spreading it into this object and then updating that specific value of that specific property that we passed in so so again you could ignore all this typescript stuff here basically this function is nice and reusable so that on any of one of my inputs I can just specify the field name and it will automatically update the state now a major difference in view is that we don't have to do all of this manually so in view on our inputs we have this V model directive so this is something provided

Chapter 8 7:01

React setField State Update

Chapter 9 7:40

v-model in Vue

by view that basically is a shorthand for specifying the value binding and The onchange Listener and so you can see that I'm actually just passing in directly form state email behind the scenes VI is going to add that change listener update the state when it needs to and this is all that I need so this is one nice thing about view is some of the stuff you do most often it provides little helpers so you don't have to write as much code so for the view example I don't have to write that custom set field function and we don't have to do an immutable State update so behind the scenes it's literally just reaching in and setting these properties it's literally saying form state. Emil equals something right so whenever that onchange event gets fired it's basically setting this to like e.

target. value but we don't have to do an immutable State update we can modify it directly and behind the scenes view is able to see when these change and it's able to render our render function here uh if those values do change now let's take a look at some of these other things here because if if this is your first time seeing a view template it probably looks pretty weird but this is how we set up Dom events right so in react we have special camel cased names for every possible Dom event on elements on insert doent name we have access to all of those and the convention and react is all of them are cam cased and then we can pass in a callback function in view we can use the at sign so whenever you see the at sign you know that this specifically is a Dom event and similarly I can do at I get access to all of those specific Dom events and actually this is a shorthand so uh without the at sign you can say V on and then specify the Dom event but we do that so often that view has provided the at sign shorthand and then anything inside these double quotes is an

Chapter 10 8:44

Vue Template Syntax

expression that's going to be evaluated whenever that event fires and so I have a helper method on blur that's going to set that specific field to blurred the other thing is if we are binding just regular Dom attributes to some expression we use a colon so by default you can see my syntax highlighter is kind of like helping us out here if I didn't have this Co in here then I would just be passing this string to Arya invalid but if I want view to actually evaluate the expression inside the double quotes then I put a CO in here and you can see that now this is going to evaluate it as an expression now in react we can just use curly braces to denote that this will be an expression so I will say in view this takes some getting used to but these are the the simple rules that you can follow if you see an at sign it's a doent if you see a colon it's being bound to an expression if you see v-h something it is a custom directive that has some built-in functionality now another thing to look at here is conditional rendering so in view if we want to conditionally render this error message here we're using v- IF and so this is another built-in directive we see that v-h and if this

Chapter 11 10:18

Conditional Rendering in Vue

evaluates to truthy it will render this element if it's falsy that element will not be rendered out in react we just use JavaScript Expressions to Define our conditional so in this case we're saying if email is truthy then we will render out the element on the right hand side and then to actually access Expressions inside of elements in view we use handlebars so the these double curly braces here are handlebars anything inside of that is an expression in jsx anything inside of the curly braces here can be an expression now the last thing I'll show you to compare here is on blur so if we look at on blur here inside of react whenever the user tabs out of a field we want to validate that one specific field set the error message if it's not invalid and then also set touch to true for that one specific property so here when we blur an input get field error is just a helper method that I wrote where you can pass in the property name and the value it's going to run the validator for that one field and give us back an error message and so if there is

Chapter 12 10:36

Conditional Rendering in React

Chapter 13 10:46

Accessing Values inside Elements

Chapter 14 11:03

onBlur helper in React

an error message that means this field is invalid if it's undefined there's no error there uh but you can see here this is another immutable State update so for the form errors we're needing to spread the previous state and then update that one specific field error and then for touched fields we need to spread the previous state and then update true for that one specific property now in view like I said we can do mutable State updates and so this is a little bit simpler because we can just reach into the form errors here and set it we don't have to do any sort of spreading orable update and same thing with touched Fields now that's most of what I want to show in this comparison there's a lot more we could dive into uh but one thing I'll talk about before we do move on is the difference in rendering between react and view so in react we have a single function here right and so anytime our state Updates this whole function is going to rerun and it's going to return a virtual Dom tree but it is important to note that every time this render function runs all of the code inside it has to run too so that

Chapter 15 11:48

onBlur helper in Vue

Chapter 16 12:01

Render Cycle Considerations in React

means we're actually like redeclaring all of these functions every time it renders but it is something you can forget about when you're working in react is like all the stuff we Define up here are function declarations they're these are going to get redefined every time this function runs again right that's just how JavaScript works and that's not necessarily a bad thing but it is important to know that that's what's happening and you do need to know that basically these things are getting recreated over and over again now in view that doesn't happen so in view our codes are split up into our script and our template our script you can think of like when the component is created so when the component is created is where we actually Define all of these State values and all of our methods but that code only ever runs once when the component is created we Define our template down here but this actually gets turned into a virtual Dom render function so I mean it looks like HTML but at the end of the day the code that you're shipping to the browser for using a build process you're getting a render function that doesn't redeclare all this stuff on every render it basically just has access to all of that stuff in scope

Chapter 17 12:51

Vue SFC Considerations

uh but still returns a virtual Dom Tre uh based on that now I'm not arguing that one is better than the other but it is a different way to think about things right because when you're working in react you always have to remember that yes this this stuff is going to run over and over again uh but in view we've kind of separated things out so we know that the stuff defined here is only going to run once and then this template is basically what's going to be updated based on if our our state changes so to summarize probably the main differences here between View and react are how we Define state so in react these are all getter Setter functions and then in view we have reactive variabl that we can just modify directly um and then in order to actually render things out we're using a different kind of syntax so in react we're using jsx which has its various rules and then in view we're using its template syntax which has its special directives and at doents and ways of binding Expressions um now for a lot of people they may stop here they may say I do not like how Vue does this so I'm not even going to touch it but uh in these other examples that I'm going to show you we're going to dive into other things that aren't just the

Chapter 18 13:30

React vs Vue - Render Cycle

Chapter 19 13:48

Vue vs React - Summary

template syntax and state up dates there are other aspects of building front-end applications where I think we can really note the differences and see the the different approaches that both View and react take thanks for watching if you like this video definitely subscribe click that notification Bell so you can be alerted next week when I drop the next video in this series where we dive into this shop example and we really demonstrate how do we share State across multiple components in our application uh I'm also going to make a plug for the syntax Snack Pack which is our bi-weekly newsletter because we have new merch coming soon that you you'll be able to buy but if you sign up for the newsletter you'll get a 15% off code and the newsletter is super cool it comes out every 2 weeks and just has a few tasty clicks few things that will keep you entertained we don't spam you it's pretty slick uh so yeah sub to the newsletter get your coupon code and keep an eye on the swag store so if you go to syntax. FM click on swag that'll take you to the shop and sometime next week you'll be able to buy this shirt and a few others as well so that's all I got

Chapter 20 14:37

Next Time

Chapter 21 14:52

Thanks!

for you I'll see you in the next one

the [Music]

Quote it with credit and a link. Quoting Syntax