Web3 Frontend Development, Part 1
Urbe Campus ETHRome Edition 2024 · October 2024 · 3:11:11
Orbulo, an Urbe co-founder who has competed in ten web3 hackathons, opens the third lesson of the Urbe Campus ETHRome edition by describing the hacker mindset. The long session then moves into building web3 frontends for hackathon projects.
Speakers
Topics
From Urbe Campus ETHRome Edition 2024, hosted by Urbe.eth.
Transcript
[Music] [Applause] [Music] okay so I think that we can start can you all hear me right okay so I'm not the kind of guy who like talks too much so um hi everyone thank you for being
here to the third lesson of the UR campus you may be wondering why fabit isn't here and why his hair maybe grown but I'm Paulo also known on the web as orbo I'm one of the co-founders of the urbas community of the e aaton and as well also I'm a co-founder of Brian which is an AI web three startup that I'm working on so but today I'm here uh mainly because I'm an AER not this kind of Aer but rather this kind of Aer the guy that sleeps during gatons and um but what does really Aker mean so I really love this definition of Aker actually I have it on the back of my of my iPhone as well and uh Aker means an individual
which is capable of solving complex non-intuitive problems in a seemingly intuitive manner it may seem complex as a definition but in reality it means someone who uh has the creativity and um loves basically what it does and it's capable of solving problems so this is a little bit of flexing of some of the acads that I've been with I participated at 10 web Tre agons I've won more than 40 bounties around $50,000 in prices and just around six hours of sleep in all the agaton so this is another map where of all the places that I visited while aring and um this is why I really love aring as well and I would like you to
start considering also as well to start talking you can see some countries like the USA have been in Denver for example uh France Spain South Korea Poland turkey and so on but you may think and ask what do you do at acons what's your job what's the actual code that you develop so my job at aons while working with lemon who's in the in the back side I mainly do the front end so how many of you are U like know what frontend development is okay so someone does and someone doesn't okay and how many of you remember the W art is old enough to
remember this kind of art as well okay not so many so a little bit of history when I started developing I was a backand developer and I really hated frontend like I really really really hated front end and uh but why because while working on the back end you basically just read and transform data so backend is what happens basically behind the curtain of the web and so that's it that's pretty easy of course there are some really complex back ends you need to handle a lot of stuff but uh at the end of the day it's just basically reading and transforming data while instead while working on the front end you need to account for different browsers devices people actually not
able to click buttons properly and much much more so it can become frustrating so that's why those are like the main reasons why uh I really hated uh working on front ends but when I started doing my first frontends uh I understood that front development means making the web a better place and I think that it's the most creative part of coding and because you can basically Express and you can actually see the code that you are developing which is something that doesn't happen in the back end so I think that it's really beautiful of course it takes a lot of creativity uh sometimes a lot of design uh if there's
not someone that works with you and does the design for you but I think that it's really beautiful so and also another thing that brought me to the front end development side uh was that being a frontend developer in the web Tu that was what I was doing basically uh transitioning from the web2 to the web tree it's easier it's easier why because the tools of the trade that you use in the web tree so HTML CSS JavaScript typescript and so on are the same so you basically use the same tools the same the same programming languages the same libraries like most of the same libraries and Frameworks and um as the web CH and also in the web3 we have a
lot of useful libraries for any basically use case of stuff that you want to develop but uh most importantly I what I loved about developing frontends in the web tree and in the blockchain space is that web3 is complex and frontend development is the only way to make it easy so it's a huge challenge for any frontend developer to um develop websites or native applications and so on to make the web tree easier and that's also called and known for the people that work in the web tree as the ux UI challenge which stands for user experience user interface challenge so what is this challenge
basically in web2 people always know like most of the times know uh with what they are interacting with so an example that I that I've written here is that when you click the plus icon in the Instagram app for example you already know that you're going to add a new photo for example or uh inside your banking app if you click on deposit you know that you're going to deposit some money okay that is not the case for the web tree because in web tree people can get lost because you have a lot of um New Concepts like swapping Bridges defi gas tokens and so on and so forth and so new users especially can get lost in this mess and so uh these are four uh
like the four sub challenges that I like to that I like to call of the uxui challenge and the first one that we need to do when we develop any kind of front end in the web tree is explaining the obvious what does it mean it means that even if something for you that you are the one developing this application or uh thinking about or edating this application you always need to explain something that you think it's obvious because it will not be most of the times for the people that are going to interact with it the second one is expressing the immutability I think that fabrio told told you about uh the the principles and the and how does the
blockchain work basically so you should know that the blockchain is immutable whatever you do stays there there and so on and so forth and so when you develop any kind of front end for the web tree you need to express the fact that any kind of action that the user is actually performing is going to be immutable whether for example is trading two tokens when you trade them there's basically no going back without of course doing the opposite transaction but the third sub challenge is impressing with creativity without distracting what does that mean when you develop front ends in web 3 uh you need to be creative you need to be uh to develop like be beautiful front ends
animations whatever but you do not need to distract the user from from the real functionalities of uh of your app basically so if you're app do something uh you should not like distract them from um what you want them to do basically and the last one uh is letting users let users explore what they're doing so this is also comes from the um property of transparency of the blockchain so most of the web tree applications allow users to explore what they're doing and what they have done in the past and uh this gives the the users
the feeling that they are interacting with a uh with an application that actually is going to interface with the with a blockchain so another few slides and then we'll go to coding so uh when you develop um a front end or an app in the web tree um those apps are usually called daps so dap so D app uh stands for decentralized application this was a concept that was born around 2016 2017 2015 like around those years and stands for as I said decentralized application but in the
past it was used to reference applications that were served in a decentralized manner like making them Unstoppable when we started developing and working in the web tree we always had this idea of since the blockchain is Unstoppable it's immutable and so on so as well the front ends need to be so that's why there was this concept of having these Unstoppable front ends in reality today the concept has lightened a lot um because due to the need of more complex application more complex use cases and with the technology going forward um now the concept of dap broadly refers to any application that
interacts with the blockchain so when you hear the the term D you can think of any application that interacts with with any blockchain basically whatever that is so uh this is what I like to build our first app you're going to use this scaffold this two which is a framework but in reality I'm going to ask you some question to understand what's your what's your level and then we we will see basically how to go forward so let's start coding so the first question that I have is how many of you know JavaScript JavaScript or typescript okay so and so okay so for the ones that
already know it it may uh how many of you that know JavaScript know react as well you can lower your hands please because I already know you so so yeah so what I suggest you like what we're going to do is that we're going to use this website that I really like and this is going to be helpful for whoever knows uh who who doesn't know uh typescript and then it's also useful in my opinion for people that already know typescript as well because it can give you uh some Refresh on how some stuff works and so these are some exercises
that we are going to do they are really fast and we're going to see them together so just for the ones that do not know JavaScript I hope that you have some concepts of uh programming languages that are not compiled so basically JavaScript is similar to python if some of you know python I think it's it's easier to understand so uh so basically JavaScript Works uh as a script so any line of code is executed basically line by line starting from the start of course of of the file you have all the variables functions that you of course have in all the programming languages and um while instead typescript is
basically you could imagine it as an extension of JavaScript that makes it more close to uh strongly typed languages like C++ or Java or whatever so I think that at the 42 you should be studying something like C++ or I don't know so what I suggest is that you go to this website which is called total typescript tocom you can uh login using your GitHub if you have one otherwise you can use your email it's free so you I think that they will send you some spam imates but you can block them that's not my fault sorry so if you can register here and then go to the
tutorials and then we will do these really fast exercises on uh on typescript and as I said they are really really really fast but they will get you to the point of understanding how the language works and so we can go forward as well and you can do all the tutorials on your own maybe when you are at home there are some example this one Zod is really useful or or as well dis solving types SK errors but the ones that we are going to see are the beginners typescript so that everybody is on the same level basically and then we will go with the react with typescript so we will understand what is react and how to start developing using react because
react and other Frameworks built on top of react are the uh state-of-the-art When developing on web tree basically there are of course other Frameworks but the most used ones are react and next which is the one that we are going to use tomorrow with uh scaffold D basically so whenever you are ready and registered please let me know if you're still doing or not every okay so what's really cool about this website is that you have a video and then you have an exercise and then you have the solution of the exercise so we
will go real fast with the with the first ones because they are really easy and we do not want to waste a lot of time but if you want to do them back on your own and as I said I think that there's also some paid stuff here I think it's worth it I don't know how much does it cost it's not sponsored by the way so so that you know so what's really cool is that you can execute everything directly from the browser so right now you do not need to install anything uh it's always ready here basically so I hope that you can that you can see I'll try to zoom in a little bit Yeah like this works so as I was saying before so typescript is like while I'm speaking you can try and
solve it on your own here below you have an error basically that tells you that this parameter has any type which is something that can be done in typescript but it's better to specify a type so in this case we need to create this function which is called add to numbers and we need to define the typing of the A and B parameters since we are starting with it to in order to define the parameters of a of um the the types sorry of parameters of a functions you need to put um um semicolon no a colon and then you need to define the type in this case
is number and we do the same with B so basically we are defining that the function add two numbers is takes a which is a number and B which is a number so I think that we just need to save it and this should be working just fine as you can see one test type check complete we finish the exercise so this was pretty pretty fast and also below you have some explanation about about the exercise but whatever we don't need to check the solution so then the second exercise is related to objects so typescript is since is a strongly type language
rather than JavaScript it works uh most of the times using OB and using interfaces and so on which are Concepts that are more like uh Native of any object oriented um programming language and um as I said we're going to go real fast with with with these ones and so in this case we have the same function which is add two numbers but instead now instead of taking the A and B parameters as before you have the params do first and params do 2 so these dot notations this dot notation in JavaScript and typescript of course I
will just say typescript so it's easier for me otherwise I I'll go m in a couple of minutes so this dot notation means that we are accessing the in this case the first variable and the second variable inside this parms and as we did before in order to define the type because it's what we're doing right now we are going to say that params is an object and we're going to use the the uh what are they called in English I don't remember yeah the curly brackets thank you and we're going to Define in as as an object containing the first variable which is a number and then the second variable which is a number as well as you can see it's not going to complain and it's
going to say that hopefully when it stops running all these test it should say that it works so basically what we have done in the first exercise and and in the second one we Define the types of an object and as you can see it's like for the ones that doesn't know typescript or JavaScript it's really easy to understand what you're trying to do so going to the third exercise instead we are going to uh get the concept of optional properties so optional properties is something that you may have inside uh typescript objects and um it's something that it's not I I
think that not so many programming languages have them like when you define an object it's either uh null or um or it has a value instead in JavaScript you have basically you can have null you can have the actual value and you can have a third type which is called undefined so it's basically when the value is not defined as the name says basically and so in this case as you can see here uh if we keep this object defined this way so we first defined as a string and last as a string when we try to pass only the first attribute which in this
case is mat is going to complain because we are not passing also the last because this is something that you you should not be doing but if I put test here it works because it wants also the second property but we cannot edit the tests and so in order to set a variable as optional what you do is you put the uh question mark after the name of the variable and that marks it as optional basically so it's really easy so it's like last as a question so I don't know why it's complaining but this should work so so yeah so adding this question mark
sets any property as optional we're going to see this other exercise which is also on optional parameters which is something that you will um find a lot inside either libraries or when you're trying to develop any kind of front end you will do a lot of checks on optional variables because it's something really really common during typescript development so this exercise is pretty much like the like the last one you just need to add um a question mark on the name of the variable as we saw before and it
basically works so this last in this case if it's not passed it's like it's like calling this function like this basically con name Paulo undefined so when you don't pass it in reality the value that is getting passed to the function it's undefined so the third exercise is assigning types to uh variable so in this case we have a question so how do we ensure that default user is the type of user at this line and not further down in the code so what does that mean in in typescript as
a strongly typed language you can assign the like the variable can get its typing in two ways the first way it can get its type it's when it gets inferred so when if you have a variable and you assign it the the value of uh a known type so a number you don't need to actually specify the type of that variable so in this case if I put three here I know that default user it's a number okay but when we're dealing with types with custom types and objects and so on if you do something like this the actually this default user it's not
any kind of specified type it's just empty brackets so we need to assign to the to this variable its type which in this case it's user and as you can see it's going to complain because when we set it the um the user type the user type has no optional parameters and so in this case we need to also assign the ID the first name the last name and the is admin so we're going to set the ID to one the N the first name to whatever you want us you can you can but an empty user
is like it's like user does not exist as null okay so you you you need to specify that it's user or null in this case but we're going to see this notation later basically you can do something like this but it would only work if everything over here was optional in this case it works but just one single required property of course and you cannot do it anymore so in this case we are going to just go real fast this should be roll and is admin of course it's true okay so that as well was really
easy uh okay so let's go to the next to the next exercise so this exercise is uh is regarding the constraint of value type so what is really cool in um in typescript is that you can um constrain the the type of a property or a variable to some fixed values in this case we have this question which is how do we ensure that the role is only one of admin user and super admin in this case both like admin user and super admin are strings so in
this case this is correct like actually R is a string but as you can see we have this well this notation over here is used to expect an error in this case and it's red because there's no error because I should not be allowed is actually a string so there's nothing wrong over here okay in order to constraint this value something which is really cool in typescript is that you can directly say that Ro for example needs to be admin or Ro needs to be user or Ro needs to be super admin so only the value that you're specifying here works but of course we need to support admin and user
as well so we are going to use the what is this word the you the or operator or like I don't know how it's called intersection operator I think and basically we're going to say that the variable role it's either admin user or super admin okay and as you can see when you go and have this intellisense over here it tells you already even the editor tells you that user do roll can can only be admin user and super admin and as you can see this expect error now works and if I remove it this is an error because the type I should not be allowed is not assignable to the type
which is either admin user or super admin basically and this is really useful because you can like something that is really powerful about typescript which is really bad in JavaScript and also Pyon as well is the fact that since those languages are not strongly typed you may Ur in errors like when the code is running and not when the code for example is being developed okay while typescript provid us to have the flexibility and the easier development of JavaScript while at the same time uh having the strength of any strongly typed language so in this case if I know
that um all the users inside my application either have the admin role or the user role I can Define this way and so while developing the code if in any part of the code I'm trying to pass something which is not admin or not user I'm going to get an error also during compilation as well so it's really useful to use types so the next exercise instead is regarding arrays which are lists hopefully all of you are familiar with with arrays let's see if we have a question and so yeah in order to Define arrays as
you can see we have this interface user over here which has an ID first name and blah blah blah and then we have these posts then we have another interface which is a way of defining types of a post of course when we try to create this user the post needs to be an array while instead here it's defined as an object so the way to define an array of any given typ type is just to add uh brackets square brackets I why I can't remember the name of the brackets today so you you just add the square brackets after the the name of the of the type and basically you are saying that this posts
parameter uh inside the user it's an array of posts basically so that's how you define arrays array typings in typescript what is really cool also in typescript is that you can say uh like you can express Express it's the wrong word you can you can annotate any function and tell uh and say what is the return type of that function which is not um like you're not forced to do it but if if you already know that that
function will always return that given type it's something to do because in this way you can always check that you're are not doing something wrong basically and so in this case we have the same structure as as as before well now you can also Define arrays like this but whatever I don't I don't like it but so the question here is how do we ensure that make user always returns a user and in in this case it's as we done already before with with properties with uh function parameters and um what you need to do is simply the column and then say that this is going to return an user so
in this case as you can see the make user function now returns an error before without this this function was completely fine and if you go over here you see that this is a function that returns basically empty brackets while like this we we need to return an user so we need to edit this return value and say okay this is one and whatever we are going to put this real fast as well so we don't lose any time we put the admin now this wants everything to be completed but uh which was title I think
okay so we need to populate it with at least one post because the test here is checking for the for the first post and oops and this works fine so as you can see in typescript you can also annotate not only the um the parameters of a function but also the return value itself now there's a like there's this concept in uh in JavaScript and typescript of of promises promises and a sync request
so to explain this concept to to the people that do not know JavaScript so basically how does JavaScript or like in in this case let's get JS which is the which is one of the basically we could call it framework but whatever it has many names where JavaScript runs on so the the node.js is basically a loop that goes around and around basically and it executes code okay in JavaScript there's this concept of promises which is basically uh code that does not block the main Loop so it's code that runs
in the background but it's not really in the background but let's not go too deep on in that because we will need to have a whole day talking about promises but it's basically code that you can let it run and you could either wait for its result or just let it go like whatever it happens it happens okay and um promises and requests as well which I hope that at least some of you have already done some API requests as well they are always done using this a sync and the await which was the the keyword that I was using before um await
basically tells um JavaScript to wait for that given promise so do not let it run basically as it is but when it finishes I want the result right away so in this case this is a function that is going to fetch Luke Skywalker I don't know how but there's this API where you can fetch data and fetch is the function that it's the native one in JavaScript that allows you to make requests if you don't specify any options it's just a get request so you it's like calling it in the in the browser and as you can see it's going to return all this data of Luke
Skywalker and um it's pretty simple but in this case so we are not going to Too Deep about it but we need to specify that the result of this function is a promise of receiving a Luke Skywalker object okay so think about what I was saying before about executing some code and then something happens and the the result is going to come that's the concept of Promise because you're going to execute this function and I'm going to promise that I will give you this kind of object that's how I like to think about promises in JavaScript but
let's not go too deep into this argument so in order to define a promise of something as you can see the interface of Promise gets this parameter T which T is um is a keyword in Java in in typescript that means a type so when you define promise you're going to Define promise with the type that that you specify inside the um the I I don't why I'm forgetting English but whatever you get the point you say promise and then you Luke Skywalker then I don't remember all the exercises so we're going to do some of
them together as well so let's see so in this case we have a set so what is a set a set I don't know if any of you is already familiar with the concept of set but a set is an array that does not contain duplicate objects so there cannot be duplicate objects inside a set basically and in this case we have a set of string because we are adding in this case Jimmy andrix and Eric Clapton and uh uh as you can see we have this test and this says this should give a type error when you try to pass a non
string so in order to solve this first one as you can see we have this when you call new set which I think you should be familiar with creating objects in other languages use the new keyword and then you create a new set when you in in typescript you can also for some objects like the promise for example you can specify what type uh this in this case the set like the items of the set need to be and as you can see in this case we have new unknown so it's unknown what's the type of the object that it's going to be in and instead it's going to be inferred
later because we are going to add in this case strings but it does not return an error if you if you add different values and and as we did before with with promises we are going to specify that this is a set of strings in this case so only strings can be allowed inside this set and of course you can do strings you could do numbers and so on but this is going to give a type error when you try to pass a non string and um whatever this is a test that checks whatever it works by the way so we saw saw also how to create this these objects while specifying the
the type inside of it in this case for for the set or for the promises instead in the next exercise we are going to see how to assign Dynamic key [Music] so Dynamic Keys also it's a really really nice concept inside inside typescript and so basically let's see what this function does so this function has a cache so a cach is basically an an object um a storage most of the time which is temporary in memory and so on where you store data and then
you can also Retreat or delete data from that storage this case this create cache function is going to return an object containing the cache itself the add remove function because inside an object in typescript you can also have properties that are defined as functions okay and so if we see this should add values to the cache is going to add the add the key because aach is a key value storage so you associate to a given key a given object or value in this case we're going to see that the add function here as a uh a string for the ID d and a string for the
value so we could specify the type of this um of this cat in this way where I think it should be it should be like no it not should be like this which is really funny if I don't if I don't remember how to how to type this one okay so I actually don't remember how to do this which is really funny but so basically let's see the solution otherwise so okay you could use this kind of okay so it it makes sense
so I I wanted to do it in a more fancy way so there's a way to define objects in which is called a record which basically a record is uh a dictionary in Python for example or in other programming languages I don't remember how it's called and so basically you say that this cash is basically an object um a record which is a key value storage the first type that you pass is the is the type of the key and the second value that you pass is the type of the of the value that you're are going to associate to it so in this case we're going to say that the cache as we said before needs to access ccept a
string key and a string value in this case we Define it like this and this should work so this is another way to define which it's not really variable but whatever so Union types let's see what's the error over here so we have this function which is called course amount and you should return the amount when passed as an object and should return the amount when passed as a number so basically this function should always return a number whether we
pass the amount as a number or this uh type which where amount is a property basically so this can be done in multiple ways in JavaScript the easier one I think so is the is by type checking so in um in typescript there's this function which is called type of is equal to number we are going to return the amount otherwise we are going to return should be like this right yeah so what we're doing is we are checking if the
type of amount it's equal to the string number in this case because type of and then a variable returns a string and so we say if the type of amount it's strictly equal which is three equal signs I think only JavaScript has those I don't think that other languages have them but I may be wrong but is strictly equal to the string number then we simply return the amount value in this case while instead of course if it's not a number it's an object in this case and so we're going to return the amount property of that given object and as you can see this works let's see if there should be also other Solutions of course
you can do it like this you can check in the other way so you you could check if the type of amount it's an object and then return the property otherwise return the amount this is what we have done and also there's this way where you could check check directly the property of the object because you could like amount do amount if you pass a number it's going to be undefined because even amount in reality it's it's an object and so you could return the amount. amount or the amount directly
then in like all the programming languages like with the most advanced ones of course we have errors as well and errors are thrown by using this this this line over here you could also specify custom errors but I don't think that it should be over here yeah and so in this case we have this function where if if we pass fail as an input it's going to throw an error what you can do is that use this TR catch statement of course it should be I don't know if any of you has never
seen a TR catch statement but hopefully you have seen it so basically what a tri catch statement does is that it tries to execute the code inside the tri brackets and then it's going to catch any error in typescript you can specify the kind of error like the kind of type of error that you are catching in this case we are we are throwing a generic error so we can specify over here that the type of what why this wants it to be like this okay yeah it it it makes sense because
error is are generic okay so errors can be as I said typed this any over here is um a type that can be used in typescript I say can because it's not the best thing to do because any means literally any anything so typescript has this concept of um a configuration file which is actually hidden over here so you cannot see it but basically inside this uh configuration file which is called TS config you can Define how the typescript compiler is going to behave on your code and um the only
place where any is allowed I think by default is in try catching instead it should be all like you should never basically use it and um and yeah so try to avoid like most of the times the any value because like the any type because it can generate errors while the your code is running another really interesting uh uh Concept in typescript is interface inheritance we have seen some types we have seen users we have seen posts and so on and
so something that that it's that it's really cool is that as you can see we have the user post and comment interfaces and all of these interfaces share the same ID property okay so what we can do is we can create another like a starting interface where it it has this ID property we could call it for example I don't know ID object we could call it we have this ID object and then we can
say that um well I would do it with with types give me one second let me know what it this what this that want to do okay so I will show you directly the solution of something over here so in this case he's going to call it as base but the idea is that when you define an any interface in J in in typescript you can extend other interfaces what does the the extend does it tells basically that this user contains all the
properties of the base interface and then the user extends it with the first name and the last name the post is going to extend it with uh the title and the body as strings and the comment as the comment basically yeah so that was basically inheritance from a starting interface I don't know why you're saying types but whatever so until right now we have seen mainly as we said um inheritant type and and so
on in this case we have the same interfaces as as as before we have the user and the post and then we have this function which tells how do we type this return statement so it's both user and an object of of posts because as you can see we do not have um a single interface containing both users and posts this can be done by saying that this is going to return something like this so we are going to say User it's going to return a user and in this case
so it's not the the or that we saw before but it's going to return a user and an object that contains posts and post as an array of course you could have a user with with post and so on which could be you could do something like this as well without creating a um a new type you could do something like this whoops and this is going to work in the same way or also if you want to use types or which is something that I like more
most you can also assign what we had before so this stuff over here you can assign it as a type so this is the like writing this or this is the same thing basically but like this you can maybe reuse it in your code and so on but I like doing it like this basically so there's multiple ways to do combination of types basically to create types the best one in reality is what I showed you by creating types so the user with posts like this and user with posts over here so you know that you're always returning
the user with posts okay so as he's saying here we saw inheritance of types we saw how to combine them in this exercise instead we need to create a new object that only includes the first name and the last name properties so we have this user and we need to Define this new my type basically that only contains uh first name and last name we're going to use should be pick we're going to use this um this helper which is called pi and P
basically allows you to to to to say I want to pick from this type the user I want to pick the first name and the last name should be like this hopefully Yeah so basically pick allows you to create um allows you to pick actually the properties that you want to to to keep from a given type it's not that common in reality to use piak but I think that maybe there's something okay there's also omit which does basically the same thing but in Reverse so basically give
me everything of user except the parameter ID with pick instead you say okay from the user I want to pick these ones so something that it's that it's that needs to be noted that you should use Peak because Peak allows you to have autoc completion in inside your editor while omit doesn't so let's see we have the last two really fast exercises and then we will start doing some more complex stuff regarding react and um we have this typ in functions problem I think that we already done something like this or no
didn't we already do typing function okay whatever so you said we have this okay this is a function so I'll use this to start explaining a little bit something about the about the web and the browser and so on so basically you can see that we we are currently calling this this object which is window so window is an object that it's uh injected inside [Music] um let me rephrase that because otherwise you will get it wrong so basically when you run JavaScript Javas JavaScript can be run both in the in the
in the back end using for example node.js and other stuff but of course it can also be run in the browser that's why most of the websites are uh using JavaScript when you run JavaScript in the browser you have this object which is called window and window is exactly what you imagine is the browser window where that code is actually running and so it's an object that has a lot of properties a lot of cool uh functions and so on and what you can do what was done in reality more in the past because right now we have a lot of helper function and so on what you can do in this case you can add event listeners to the to the window
page what does that mean is that when a given event is happening inside the window it's going to trigger the function that we are going to um that we're going to Define over here so in this case we are adding an event listener whenever the windows is focused so whenever the window is actually active and blur instead it's the um it's the opposite of focus so you when you blur something you uh release the focus basically from it and there are a lot of events and they should be also T typed over here hopefully if we have them as autoc
completion but no sadly but yeah there are a lot of events in this case what this exercise does is that whenever we add this event listener to the window so whenever we focus the window we want to trigger this function and pass it through uh instead when we blur the window so when we are not interacting with the window anymore we want to send fals to the on Focus function so in this case we need to define the in this case what it's passing it okay so the ad listener function takes a
function as an argument this is something that you can do in JavaScript and yeah and so typing a function as a as a parameter can be done in multiple ways the the easier way is doing it in line in this case and so what you do is basically you type the function over here so you say okay on Focus change is going to get a is focus for example parameter which is a Boolean and it's going to return in this
case nothing hopefully nothing okay because this one void so basically we specified that the ad listener function takes an onfocus change function on its own that gets an is focus parameter which which is bullan and in this case returns uh void because this function that we are passing over here it's not returning anything if in this case this was to for example return the why I can type today the is focused this should give an error
somewhere but maybe since we passed the exercise is not checking anymore yeah but you you get the idea in in the case we change the return value in this case from the void to Boolean void means nothing basically we can change it over here and this would work at the same time so let's remove this and then we have the pretty much the the same thing but using a sync function so we will go real fast with this as well in this case we have two functions as you can see this is how you would
pass them using this notation over here which is really brutal but whatever in this case we have a create user and a get user as we can see the create user takes no parameters so and returns hopefully a user and this wants it to be like this as we did before this is a function that gets no parameter and Returns the promise of actually a string in this case because the create user is going to return a user ID and then the get user instead is a function that gets the user ID and returns a promise of a user so it's the same that
we did before what we added instead the is the result is no longer directly a string or a user but it's a promise of a string and a promise of a user so this was real fast uh a really fast deep dive into how typescript works and what the types are and so on and so forth and I know that maybe it can seem confusing to you if you have never used it but if you have any questions if you mainly to the ones that do not know JavaScript or typescript is something it's not clear let me know because we're are going to move on to something which is more complex but it's basic basically it's
programming so if you understand the programming language you will get to work basically also with the with the framework so if you have any question feel free I will ask them I do not eat people yet amazing so as I said at the start we we're going to see the beginnner typescript examples which were really easy instead we're going to go to the react and typescript ones as I said before why react because as I said if you are looking into
working into frontend development whether it's Web Two or web 3 reacted still in my opinion and I said it's my personal opinion it's uh it's the best framework both to start with and to uh start developing also really complex projects you can do some easy stuff with it but you can do also uh complex stuff using animations and um so on and so forth so we have the same logic as before we have all these um hopefully there are some exercises there should be exercises I think in the okay in the components there are
some let me just see one second because I did not check this in advance if we can do them over here yeah [Music] okay just give me one second I'm thinking on how to structure this one I don't know if to start from here
or maybe from in general web development but no okay yeah so let's start with with react but more in general with the concept of web development so as I was saying before JavaScript code can be run both in the in the browser and in the in the backend of course you can type your own JavaScript code and your own HTML and so on and just basically start developing
on it you do not need any kind of framework you do not really need react or nextjs to develop something but of course Frameworks exist to help you build more complex um more complex applications in regards of what actually um a web pag is I wanted to show you if you go on on your browser and you inspect the content of of a web page what you actually see is this like this is how it gets rendered like you have this really cool
website with the video and so on but in reality what it is behind the hood is just a huge uh HTML f file which I wanted to open okay like this so the browser is capable of uh interpreting the three main components of any web page which are HTML CSS and JavaScript so HTML it's not properly a programming language but it's rather a markup language so a markup language like uh XML for examples are languages that are used to define mainly the
structure of something in this case the structure of a uh of a web page so HTML is used to create the the structure to Define where the objects uh should be placed at the start and uh so on and so forth and uh CSS instead stands for cascading stylesheets and basically CSS is what makes a page beautiful so it's all the Styles the colors and um and so on and so forth you can also do animations as well with CSS and the last component which was what we were looking until right now is
Javascript instead JavaScript adds the logic to uh to the page so whenever you click a button whenever you I don't know you scroll somewhere or whenever you FOC an input a textual input something happens and so on and so forth so we have HTML for the structure CSS for the for the design and for the and for the graphic and then um JavaScript for the logic when the web started uh we had this really ugly uh let me see if we can find some some screenshots of really old really old websites
like this one from this was Yahoo it was really ugly and not only ugly but they were not really functional why because we started as it's saying over here basically it was plain HTML and plain HTML is just ugly what does that mean is that if I can let me just do something real fast just wanted to show you something
regarding HTML for the ones that I've never seen it so HTML looks a little bit I'm going to zoom now worries so this is the basic structure of HTML which is the doc type HTML can be put and can also be avoided but whatever it's better to specify it and then HTML works by using tags so tags are basically you define what the element is and then you need to close it on the other side in the case the element contains other elements so in this case
the HTML it's always the HTML element it's always the root element of any HTML document and as I'm showing to you H HTML as any other language even if it's not a programming language it's just basically text and then you have these other two main um main elements which are the head and the body the head it's an element where you define the metadata of the the page so whatever you put inside the head it's not actually shown in the uh in the content of the page but rather it's in the head you put for example the title that shows in the
browser or you can put some special elements that whenever you share this page on Twitter for example is going to show a particular image or when you share it on Facebook uh shows another image and so on and so forth so this case we can to give the title to the page we have this this title element and here we can say for example urbe campus 2024 and what's really cool about HTML is that you can actually open it as a file and as you can see since the body it's empty the page is empty but you can see the title over here which urbe campus
20124 and if I change it to I don't know to eom 2024 and I save it and I refresh the title P the the title of the page changes so this add is used for everything which is outside of the content instead for the body the body is contains everything that gets shown in the page itself and here there are a lot of different elements we are not going to see all of them for whoever is not familiar with HTML but there are there are some elements which are going to be used the most and
which are for example H ones or basically any H element as you can see we have different ones and uh you go from one to six and it's a it's more of a matter of importance inside the page so the H1 is the more important so it should be always the biggest H2 is the second one and so on and so forth so if we put ampus 2024 example H2 and we refresh this page you can see that H1 is the biggest one H2 is the second and you can imagine what comes if we put H3 as well so it's an order of importance and when
you're going to use them inside also framework remember that there are it's if you're starting with it you can also forget it but when you start becoming more uh and more used to frontend development these tags are always these elements are always are also sorry uh used by thirdparty softwares that help for example people that have accessibility issues or maybe blind people and so on and so forth there are a lot of uh ways that you can help them uh to better read your page even if they are for example not able to see of the or if they are visually impaired there are softwares that read the tags and
then based on your tags they're going to give emphasis to the page itself so please do not use H1s like like text okay so let's give importance to any to like like to all the to all the elements another element is the P which P stands for paragraph which is basically text is the plain text let me zoom in so you can understand so you have paragraph and these are the main ones that I use for text you can also um there's also this one which is called the span and basically span it's an element that can be put
inside it can be put everywhere but the core logic is that it's it's getting put inside any other any other par paragraph and basically allows you to do some cool stuff like for example changing the the boldness of the text only for that given um for for that given part of the of the paragraph in this case it's not going to change the boldness because if we want to change it we need to use the B which stands for bold and in this case test gets bold but you will never use in my opinion these ones if you're using any kind of framework or or Styles as well so we can ignore this one I'm giving you I know that maybe someone of you already
knows HTML but I need to have everybody on the same line otherwise when we're going to do the react stuff and the nextjs stuff it's going to get messy so [Music] uh let's keep it like this because there there's also this div element so the div as it says over here it's an element that has no special meaning and so you could use it basically for everything it's not the best because for example if you are creating a section inside your web page you should be using the section element but basically the section and the div
can like the the div can act also as a session and as a section sorry and so on and so forth so the div let's keep it as a generic element that that can contain other elements in this case we have sub bedding and this another paragraph are contained inside the div so this is basically real fast how HTML works and I wanted to introduce you to this kind of notation using this uh using the HTML elements also I want to show you this as well when an element does not contain any child in
this case it's the BR which basically represents a line break you can instead of having it like oops like this okay this works in the same way you can close it like this so it's a shortcut basically to tell them that it's going to be closed like this and this notation is useful because for what we going to see after in react what react allows us to do is create new elements which are called components which are going to be based on this kind of structure so using react
we are going to create this as these components which will be uh containers of HTML JavaScript and uh CSS code instead regarding CSS CSS it's way way way more complex than uh any kind of like in my opinion is the most complex one to understand and and to master so in reality I do not really want to talk about CSS because otherwise it would get pretty boring but I want to show you something that
it's like when it came out it wasn't it wasn't really loved by the by the front end Community because there has always been this concept that you are a good uh web developer if you can write your own CSS so if you are able to create like this crazy files with all types of colors and stuff but the more that the time has went on a lot of um a lot of Frameworks like CSS Frameworks C came out that allowed people to uh use and uh style their website in a more easier way and Tailwind in in my opinion
is the it's the best one and it's the easiest one because Tailwind uses the concept of classes so basically in HTML which is something that I need to I need to explain you you can like any any element can have multiple properties as you would have for example in JavaScript object and one of these properties is the class so basically a class is just a string basically and uh inside this string you can Define one or more classes classes are basically just names that you give
to uh CSS classes and a CSS class is a uh is an is an object that contains one or multiple styles for example a class can contain um the definition that all the elements that have this class it's I think it's easier if I show you without using it but for example I'm going to use this font Boldt class this is how you define a class in uh in in CSS so using this dot notation so this is a simple class and this is if I want to create this class to say okay all the
elements that have this class the weight of the font so how the font how the font is going to look uh needs to be bold so in this case there's this um property let's call it CSS property uh which is called font font weight and I'm going to say bold okay so I've just created this CSS class and in order to apply a class or multiple classes to um an HTML element I just need to specify o over here of course I would need to also import the the the stylesheet containing the CSS and this is how you do it so
basically you you use the link element and you say that it's I'm going to link a stylesheet and the href which is basically where it's located is going to be index. CSS you could also type it like this so it's relative so it's better and if we go in this case the H1 it's already bold so we will not see any change but if we say class font bold here for example as you can see the paragraph has changed so we just assigned paragraph not bolt and this is the paragraph bolt as you can see using classes I've just assigned this so Tailwind basically takes this concept
of classes to another level and basically provides you already with a lot of utilities and classes that you can use to build pretty much whatever you want so you still need to understand how CSS Works to a certain degree but at the same time you are not the one creating the classes so if I go over here and search for example for font weight I have already all the classes ready so when I use Tailwind I can just uh of course there's all a matter of important Tailwind in your in in your project and so on but we're going to see that uh I don't think today but tomorrow um I think it's already
available inside scaffold it but um using classes via Tailwind basically you have all the proper basically utilities of whatever you need so in case you want a phone to be bold you already have the class ready if you want to I don't know align the text to the center in this case we already have the text Center and basically you get the point without me going through all the different basically possible classes so uh this is something that I would like you if you take a look into it mainly by understanding not the
yeah yeah so you just write into the HTML in the class you you write like the like this yeah so basically if I want this this this thing to be font ball text Center I want the text to be 20 for Pixel and so on I can type it right here the skip the Cs Yeah so basically I skip all the CSS Yeah so basically you type it like directly in the code which of course it's not the best because yeah because yeah because you
will start getting for example let meag one have write all the yeah yes so that's that's the main that's the main um the main point I want to show you something let me see where I can get it I simp put the builders web okay so this is how basically hopefully you can see so as you can see it can become
something complex okay so for example here just to give you an idea this means that I have a a padding on the horizontal axis so it's a px of 12 and this notation over here means that when the screen is from medium to large so when I'm for example on a on a laptop or I think that an iPad should be SM so when I'm on a laptop or on a big screen the padding is bigger it becomes 16 like doing this stuff if I was to do it via CSS it would get me a lot of time because I would need to pretty much recreate what has already
been done and as I said it it's it's messy but thankfully hopefully you like create a class for like all those sets you you could you could you could create uh yeah you could create some custom classes and some custom Styles but the fact is that at the start it can seem like strange to be writing this really really long stuff about whatever this is margin and so but when you start start getting used to it and really
understand what you're doing with it um it really speeds up the development I'm not saying that it's better or or that it's worse than CSS because it has its pros and its cons I personally like it more because since um I'm not really a design guy so I'm more I work more on the on the on the front and can do designs but in if I could do designs maybe I would work more on the CSS because it's easier for example to use some softwares where you can design a web page like figma for example and then export the CSS and so on but if you are more on a on the
developer point of view and you are over here like creating sections and stuff and code being able to type directly what you want inside the same uh inside the same element without needing to go to the CSS and then repport it and so on it's it's better and then something that has um something that has allowed more people to uh use Tailwind instead of plain CSS is the fact that as I was saying before instead of re reusing classes using CSS by using Frameworks like act you could start reusing components so
instead of basically let me show you this is something that we will see right now hopefully I have something over here so this may seem overwhelming but bear with me for one second let me I'm just going to show you this is the uh the the the website that I was showing you the code Okay so in this case this file over here this HTML this long stuff okay is this element over here of the page okay in in HTML if I wanted to uh if if
I was using basically HTML or Javas HTML and CSS because you could do it with JavaScript because it's actually what I'm doing right here but if I was using plain HTML and CSS I would need to basically create Seven Elements and repeat all the Tailwind stuff so I what you were saying before was right but basically by using react or JavaScript I can create the single component and then repeat it so the repetition is instead of being at a at a class level or a styling level you you can uh remove it by basically creating components that share the same style and the style is
actually written as Tailwind so in this case I have this steam card and then as I said this is react notation but the this is a react component and react component are functions that return HTML actually they return jsx or TSX which is a um an extension of basically HTML because you can have JavaScript or typescript inside of it and in this case by using react components I can pass a member object which was what I was showing you before and a member object in this case is a name a description a Twitter foraster and image and
then this theme card is then used over here to basically show all the members of the team so I'm not actually repeating the the style in some way so I'm creating it once so I'm kind of like mitigating that problem that you were saying because if multiple stuff share the same style you would get real messy so that's why Frameworks like react comes come to help basically because uh now I don't need to repeat it for a lot of files because I can always reuse the steam card component pretty much everywhere and so I wanted to share you the the the Tailwind link because so
that you can see all of you if maybe there should be something yeah I will just PL sh you this one are you on Discord right okay so I will show you this one so that you can take a look into it because as I said it's the most used one and if you are starting with front end development you don't need to start and get your head around CSS because otherwise it would take you a lot of time so it's better to understand some concepts of CSS which are
mainly um how to basically lay out a page how to have how to use containers how to use columns grids and so on and so forth and then use tail winning you st win basically that's my that's my guest so um as I said HTML CSS and yeah we're going to see JavaScript but I don't want to I don't want to talk about uh JavaScript inside HTML because it would get because it's kind of boring like
I can give you a rough idea on how it works how the actual JavaScript is uh how JavaScript Works inside of a of a web page in this case we have the the website that I that I was showing you before and as you can see there's all these script tags as you can see and as you can imagine script tags is where JavaScript lives in the browser so if I want wanted to do over here some JavaScript I could completely type it over here and if I go on the page now I just basically said log in the browser this hello word if I refresh the page I
can see my conso the hello word so basically JavaScript leaves inside script tags multiple script tags and when you use a framework in the case of next over here well next is a little bit more complicated because it uses chunks and complex stuff so let me see if I can if I can find something that does not use next let me see just one second if we have something here have something which is
plained react but I don't think so no whatever so let's let's ignore basically next but so Frameworks what they do is they allow you to um develop as I said before mainly component stuff let's talk about react you are going to develop these components you're going to develop Pages which basically are components themselves but they contain uh all the other components that you that you've developed you can add logic inside components and so on and
the Frameworks usually uh without usually actually they basically at the end of the day the result of a framework like the output of the framework is a is an HTML page okay so it's an it's an HTML page where basically the script that the framework is going to create is the one that is going to uh be responsible for handling all the logic that you've written so the click of the buttons the uh switch of the pages and uh so on and so forth and um I really wanted to show you but don't remember how to find it maybe if I
go over here maybe this because this is next as well too many people using next sadly but okay yeah so I think that we can make have a break right now and then we will start looking into the react stuff and uh if you don't have any questions of course we will start seeing our react works and um yeah you can go in peace so I've sent in the in the Discord
this uh G page this one the NVM sh one if you open it you will go to this page you will need to install this and VM which is basically um a tool to install npm and node and everything that needs to be uh installed to create and to run for example react apps or in this case nextjs apps we're going to use directly nextjs which is uh I will explain you what next nextjs is but um the front end part of nextjs is done in react so so uh
by developing on next we're also going to see some react concept so please use these scripts like follow this guide here to install MVM and uh okay because you have Windows it's better to use Linux in my opinion than than Windows there should be MVM on Windows but like I've never done it so I don't know maybe it's over here
but yeah no no no it doesn't it's better to use Linux yeah the link is as I said before on Discord is this one so you can open it from Discord and we will go to this page if you get a command not found make
sure to use the source command on your terminal on Linux and also on mechos so if you have some problems you have the troubleshooting below but shouldn't have any problem hopefully everybody done
it yes no yeah and then you need need to try and use NVM like check if it works basically if you have it installed or not when you have it installed uh mm install when when you have it installed you need to run this command which is nbm install node it's going to install you the latest version of node on your computer
and when this is done you should be able to type both node and npm as well these two commands over here after you have done the NVM install node should come with should come with with node so if when you inst node you should also get mpm it stands for node package manager yeah
yes yeah yeah yeah you need to use let me see yeah for what Linux okay so MVM install node MVM use node and then you should be able to use
both node and mpm commands so this basically node is the is the framework that allows you to run uh JavaScript code in the in the back end while npm instead is used to install all the different packages inside um a JavaScript project basically so I will add the commands over here MVM install no MVM use not this commands
no [Music] framework node JavaScript everybody any issues for I'm going to type another command I will
put it on Discord and then show it over here we are going to call it let me see okay so let's call it I've sent this command also on uh on Discord this execute it please in a folder where like you have your projects or whatever don't of course create it anywhere but what this command does is basically is going to create a nextjs application with this name in this case it's front
endend Workshop it's going to use typescript and it's going to use asint which uh it's basically um a way to it's a plug-in you may call it it's a library actually for typescript that allows you allows us to develop better code basically so it enforces us some stying some commas and stuff like that so but no worry so when you have everything installed both node npm and NVM you should be able to run this command and this is going to create the next Pro next next JS project for us so we're going to click okay to proceed and
just say just press enter to any option so don't add anything else so it's yes no yes no as you can see from over here as you can see we have we have react if we are using next JS so while we wait for this I'm going to talk a little bit about nextjs next J next js. org should be the website okay so basically nextjs is uh front end framework but it's actually not only a front end framework it's also a backend framework so
we may call it a full stock framework so it allows you to both develop front ends back ends and of course at the same time front ends and back ends so it's really useful because it uses JavaScript for both for both the back end and for the front end and for the front end as I already mentioned many times it uses react components to build the web pages and so on and so forth for so once you know JavaScript while using nextjs you can basically uh build whatever you want and nowadays it has become pretty much the standard also
because uh it's it's an open source project but no actually it's not really open source but um it's been developed by this company which is called versel and I will type it this in the case of my projects but let me see if I can go to the versel website so versel is this company that has developed this nextjs uh framework and what's really cool about verel is that uh it allows you to uh deploy your next JS applications so in a matter of minutes uh after you um after you develop something and it's
ready you can publish it on versel you will get your own domain and stuff of course not you will get a versel subdomain but it will be published on the internet so basically whenever you finish something you could put it up real fast and it's optimized of course for nextjs because they developed it so uh it's really useful so even for prototypes for playing around with it and so on and so forth it also has some paid features but we don't want to talk about them so everybody did run this command everybody has this front end Workshop
created if you're checking for the command it's available on Discord it's asking would you like to customize the default importas that supp to the alas it's no it's yes yes no yes no so it's the for the last question is no you could just Spam enter and it should work I'm just curious what so the importas is when you you can use input statement for the yeah so it's really nice because basically I don't think it's available anywhere here because this is empty but if we had another folder over
here and uh we wanted to import something we could use the uh add sign for the root or you could change it to whatever you want I always keep it to the add sign because it's it's more common so yeah let me get so we all have the nextjs application so nextjs Works uh by using something which is called a file system router so basically you define the pages of let let's go
back let's first of all let's start the the app so that you can see it running on your browser in order to run it you need to execute npm run let me clear this one you can run npm run Dev in your terminal inside of course the folder of your project and this is going to uh start the local server of um your nextjs app this is a development server and it's really cool because it has a feature which is called hot reload so basically whenever you do some changes to your code the server is going to restart automatically and then
you can like you you don't need to stop the server and restart it every time so after you run the npm randev you can open your browser at local OS 3000 and you should see this page over here I don't know what's happening so so you have this you have this page and the first thing that we need to talk about is how next Js our nextjs endles the the routing of the okay so it's keep keeps going amazing
Fantasia okay so what I was saying so basically nexts uses something called a file system router to Define Pages what does that mean is that we have this app folder as you can see and the app folder you need to imagine it as the root of uh of all your pages so in the the app corresponds to the uh in this case to the to the slash Okay so so if we were writing for example local OS uh column 3000 slash it corresponds to this page if I were to had for example you do not need to do it is just to show you
what I mean if I create for example a dashboard folder and I actually copy I can copy the entire page and if I put a page. TSX inside this dashboard if I go to slash dashboard it's going to show me the same page so basically the the structure of uh of your website is defined by how you create these folders okay and of course you can use also uh something which are called Dynamic routes which are Define in this way which could be for example uh let's say for example products I can create a folder called for example product
ID and now just just to show you what I what I mean um I can go to products this would be the ID of the product for example and as you can see I still have the same the same page because I just copy and pasted the own but if I delete pretty much everything from here you would see an empty page so this is what a file system router basically does so you create the folders yeah I'll finish one second you create the fold folders and the folders Define the path of the app yeah how can I get to that because I don't
know I just I just copied this this page. TSX file but as I said it's just to show you the you you you do not need to do it but basically the idea is that you create these folders the folder that you create for example dashboard is the is the path in the in the browser and the inside that folder there needs to be a page. ttsx where you actually create the component for that given page also there are some uh custom or I don't know maybe special is better um components in this case there's the layout component and the layout component as the name
does is as the name suggests basically provides a layout for all the compon for all the pages that are in that given folder so let me delete this dashboard because it's useless right now and also these products in this case like starting from a plane app like this we start with a layout TSX and a page. TSX in the layout as you can see we have the HTML embody elements because as I was saying before and uh in the case you missed it uh basically react and in this case next C yes that uses react uh allows you to create
components components are functions or classes but it let's keep it simple and uh remain with functions it's better to always create functional components um basic basically components are functions that return HTML it's not properly HTML it's actually in this case is TSX which is an enhanced version of HTML because you can use some uh custom syntax you can uh do some interpolation like this for example this is something that you cannot do in basic HTML so it's an
extension and uh one difference one main difference that you have between react uh TSX and um and plain HTML is that the class the class attribute that we the class property that we were seeing before in in react is called class name so that's the only difference as you can see this works in the same way but in react is called class name there's a whole history behind it and it's basically on how react works but um let's let's assume that they are the same and when you work with react just use class name instead of class because class is actually ignored and
um so yeah as you can see what I what I um what we installed like this basically starter is a nextjs plus uh Tailwind starter because it was taken from this page that it's available on Discord over here you have this page and uh after we execute this command we need to perform the setup of Tailwind this this is something that we will not do tomorrow when we we are going to use the the scaffold di because scaffold di which is that framework that I was telling you at
the start of the lesson is the one that uh has all the web threee stuff already installed and so on it also comes with Tailwind but in this case you need to uh perform these other two commands and some other configuration it's really fast so you can do it you can stop the server and install these libraries as well should be fast as I said and then I will explain you what all of this does where is it ah okay no no no just wait a second I think that it's already available
no just give me one second no okay a yeah twin was already installed okay no no you you do not need to perform this two these ones if you already installed them there's no worries you will find an extra file this ta with. config JS you can delete it in the case you have already like you followed the installation and also this post CSS config.js you can delete those because you should have already them you should already have the MJS and the TS file so yeah I think that they need to update this guide because it should be updated so so it's actually already set
up so it's amazing and um let's start talking about a little bit about about react components and how they work so what we're going to do is we're going to create outside of the app folder because as I as I was saying before everything that gets created like the folders that get gets created under the app are for the file system router so it's how uh it's all the pages of your of your app so we're going to create outside there so in the root of our project we're going to create a components folder and in this case let's start creating the um for example the adder of our website okay so we're going
to create an header. TSX file so as I was saying F uh components in uh react are functions so and components always start with a capital letter so whatever there's no component that can start with a lowercase letter so in this case we are going to create something like this this is how you create functions in in um in JavaScript you could do it also uh you could do also um you could do it also like this these two are
actively the same I prefer this one because I think that it looks prettier in my opinion and uh what we need to do is export this component from this file okay because of course without the export keyword this component only lives inside this page we cannot import it from other pages so it would be useless so we're going to add the export um keyword at the start so if we go back on our home we can pretty much you can delete everything I completely delete everything and
just add an empty div like this to show you the the real power of uh components what what we're going to do is we are going to import a component inside our page as if it was an HTML element and this is what I was saying before so basically I have I have so this definition over here basically creates as if it was a new HTML element but in reality it's as I said a react component and if we go on our page of course the server now is stopped I need
to rerun it if we go on the on the elements ignore all these scripts and stuff we have a a div which is this one and then we have this other which is which is actually a div and as you can see we have another div inside so what we're doing is basically created this creating this scaffolding of HTML elements and to show you if we add a class name for example leather in this case it's just to show you this is actually our Eder so react is capable of recreating the HTML of the page
alongside with the scripts the JavaScript and so on uh the Styles and so on and um it recreates completely the HTML page what it's really useful about components is that as you may imagine they can be reused in multiple pages so for example if you want your Adder to be available on your homepage but also on your about page you could import it in both but actually the best way when using for example like when creating an Adder for example is to import it in the in the layout because the layout is what it's what it's um it's a functional component that it's executed before all of its
children in this case the children it's just the page so the own page so what we could do is instead is instead of importing it directly in the page we're going to put it inside the the layout so all the pages that will share this layout we have the other okay if hopefully it it it makes sense so you have a layout which is the father and then you have the page and you you can also have as I showed you before multiple Pages all the pages inside the the folders will share the same layout so all the pages will have the adder as you can see the adder right now
is no longer inside the div but it's actually outside okay so it's right after the body uh uh okay so what we can do right now it's something that I want to show you which is also more react related rather than the styling because I think it's more useful for what we we need to do tomorrow so going to get okay so react has a really important uh
con a really important concept which is called hooks so hooks are basically uh functions that you call inside your components and they do all different kind of stuff there are a lot of different oops Zoom too much there are a lot of different Hooks and each chuk has its own uses and so on but the most used ones are use effect uh use memo use ref and use state so we're going to see them one by one so I can show you real fast how they Works how they work so the first one that we're going to use is the
use State hook and as the name suggests the use state is used to manage the state of something inside your component in this case we are going to say say for example counter set counter and we're going to import the use State hook so as I said before it's a function in this case it gets um a starting value because we want to implement a counter in this case and this notation over here is a shortcut basically because the UST State function actually returns an array an array of results by using this ation using the square brackets we are going
to say that the first parameter it's counter which is the value that is going to be increased starting from zero and then there's the set counter function which instead is the function that is going to modify the state of such counter and um the UST state is used pretty much in any react application and uh we're going to create create a really simple button by using the button tag we're going to call it increase and we are going to create also this H1 element that contains we um in react we use this uh the curly brackets
to uh do interpolation so basically to inject in the page um the value of something in the form of a string in this case we are going to inject the value of counter so zero is going to appear yeah sorry a little thing that I have missed in in nextjs since it's a it's a a framework that can manage both client and server side stuff you need to Define which components uh are executed on the client only this is we're going to see this more tomorrow but bear with me you need to add this use client directive on top
and this tells nextjs that this is a client only component because you cannot use react HS in in the server because of course react is a front-end uh framework so you need to specify the use client and this should work and as you can see we have the counter at zero and we have this button increase okay which is what we did over here so what I've added here is um an on click so in react all the elements but yeah all the elements have a lot of on functions these on functions are basically like on properties sorry
and um on all these own properties you can assign functions what does it mean is that for each of these events because of are actually events that happen in the browser for example on Focus as we saw before or on key down which means when a keyboard gets pressed but of course it doesn't work on buttons and the onclick which is this one allows us to do something whenever this button gets clicked okay so you pass a function in this case it's an empty function you could also get the event but we don't needed right nowor the what
Sor ah yeah you just need to import it and you import it from here in this case we're going to use the Bas root SL component SL header and then you use it what sorry yeah yeah only only layout and then it's going to be available everywhere so the console log is console.log it's the print of JavaScript and in this case it's going to log in the in the console of the browser in order to open it you can go to the uh should be tools I think tools developer tools and you can go in the console you can clear using this button
and and if I click you can see that it's going to show increase counter what we want to do instead of of course loging something we want to to actually increase the counter itself so what we're going to say is that we're going to call the set counter function and pass it as a value the current value of the counter plus one okay so every time the user clicks the counter gets updated okay so it's pretty easy so we want to modify this value with this value over here which is the counter plus one so every time I click counter the counter is going to increase of course this is something that leaves only in the client and only for this particular session or page
because if I refresh the counter goes back to zero okay so there's no such persistence or whatever there are some ways to have it but it's still confined to the to the scope of the application if you close and reopen the browser this will not remain so this is something if I refresh goes away and uh the use state is really powerful because of course you need to imagine it not only something to save a number but for example you could use something to um to store a a first name of somebody so in this case we're going to Define this as a you can also pass the
the type sorry of the of the variable as I said it can be infer but it's always better to specified in this case we we're going to set this first name and what it's really cool is that if you use an input which is the HTML element for uh textual inputs well it can be text or it can be also numbers and so on if you create let's create it without anything and say enter your name this is pretty plain input text as you can see we have the enter your name I can type something over here blah blah blah what it's if we add for example
first name here as you can see first name does not get does not get does not get populated because we did not link it to any event we are not calling the set first name and so on so instead of using the on click of course because when you type inside of an input text You' not have any on click what we do is that we use the onchange and the on change we are going to get um an HTML HTML I don't remember the type it should be um
don't remember what's the type of this one Whatever without losing time let's set it to to any and so the unchange instead what it does is um whenever the content of this input changes do something okay in this case there's this uh we're going to use the event I need to check what's the uh HTML input event I don't remember the the type of of the
event okay it's form event should be for event right okay so basically the unchange it's used on elements that appear inside the form so whether it's uh a checkbox whether it's a radio button an input and so on and uh in this case this is going to let me comment this and I'm going to show you what the event actually looks like like so and how it gets fired basically so if I clear this whenever
I'm typing something I know that it's not showing the the text but hopefully you can see it anytime I add something inside this is going to log the event now we do not to go deep into what this event is but if we go to current Target if we log the current Target of the event which means the element that has triggered that given event if I clear this as you can see this is the event this is the target which is our input uh field and if we go to Value this is going to log what what I'm typing
basically and so by by combining this uh ecurrent target. value with the use state so we remove this and we set the first name to whatever we are typing basically if I start typing you can see that it gets replicated inside the the first name variable so this is useful for example when you're building a form you can store all the different variables inside uh all all the different fields of the form inside a US state and then for example use an onclick in in a in a button to send it to your backend or to any kind of server and store it in a database basically um so this was a pretty brief regarding
the use State another really important hook in react is the use effect the use effect does not return anything and um instead takes can take two parameters the first one is a is a function while the second one it's called um a dependency array let's start without any dependency array and see what this does so use effect C going to clear this and as you can see
whenever I do something I the use effect gets triggered so if I increase the button if I do something this function is going to trigger the dependency what it does is you can uh execute the this function whenever one or more of the dependencies that you specify there change so in this case I want to call this function only when the counter changes not when the first name changes so when I when I enter of course gets called because the counter it's zero but now if I type nothing happens but if I click the button the
use effect is called so it's some uh it's it's something that gets called after something changes basically in this case it's only for the counter instead if you if you pass an empty array which means not like this because this means it gets triggered every time something changes instead if you pass an empty array like this the use effect gets called only when the comp component is mounted mounted what does it mean uh it means when the react and all the JavaScript has uh finally um injected your component inside the page so when the component
appears actively in the page in in this case it's called two times but this is a matter of the development environment and so on so actually in production when the application it's built is going to be called only once so in development sadly get it gets called twice so now if I change this if I change this nothing happens and uh the power of use effect is uh main it's mainly used in this form so with an empty dependency array in order to do something when the component enters so you can imagine like um I want to create this component that uh when it gets loaded inside inside the
page um it recovers the user data from a database and then shows the user card for example so when the component gets mounted I'm going to make a call to my server to my backend I'm going to retrieve the data and when it's finished okay I'm going to store it inside the inside my state so let's imagine for example um actually I can show you because I I can use what DB do you recommend for for front head set we we do not have DBS but you can uh I in my opinion my opinion postgress if you
want yeah and um okay let's let's talk a little bit about since we are talking about next I'm going to show you also what's the powerful thing about uh next being both uh front end and back end so by following the same logic that I was saying before about the uh file system route so basically there's a special folder name that uh if you put it inside uh app it's going to enable the back end of your nextjs app in fact if you create a new folder inside up and call it
API and inside here you still follow the same file system route that I was saying before in this case I'm going to call Ito and then I'm going to create a route. TS file what I've basically done is I've defined the first API of my of my front end I don't want to go too deep about it because this is mainly about front end but what what I'm going to do is I'm going to create um I'm going to create real fast uh an an API so that we can return for example
the um the first name which is going to be Paulo this is just to show you how to call from the front end so okay so this should work so if I go SL API SL shellow as you can see this Returns the object that I've just defined here as I said I won't go deeper on the API side because it's not on the scope but as you can see you can also do API with nextjs and what it's really cool is that you can call the same apis from your front end okay so you could do in this case
fetch API low then we're going to get the J which is the result of the API and then we're going to set so basically what I'm doing is that I'm calling the/ API low then I'm parsing the result into adjon which is the result of my of my API and then I'm going to set the first name that comes inside the data which is this one I'm going to set it as Paulo so as you can see whenever I enter the first name gets populated with the data coming from the API so that's
the powerful of like the the power of the of the of the use effect without any dependency of course if this would have had any dependency inside its dependency array when I enter yeah now because it gets set to Z but whenever I click but doesn't show but whenever I click you can see that an API call gets gets made basically so whenever I increase in this case I'm I'm calling my API um so we have the use effect the use the and the use counter which are which the
the use State and the and use effect which are the most important ones there are some others that are more um um they are more deep and we do not need to look into them right now as I was saying before there's the user F which basically allows you to to get the reference of um an element or you could or there's also the use context but there's no need right now to see these ones uh but what it's really cool about uh react is that you do not only use the default um hooks you can also create hooks on your own because hooks are
basically just functions so since we can do pretty much everything we can also create our own hooks so if we create a folder and call it hooks and let's call it use Paulo use Ur instead can be also so as I said we can create our custom hooks custom hooks can be used to do whatever we want so for example if we have inside our application we we don't we do not want to every time uh fetch a
given information or uh perform some kind of logic using JavaScript we can basically bundle it inside our own hook and then just call our own hook pretty much everywhere in order to show it to you really easy we're going to create a function as we were doing before which is in this case it's return return Ur use Ur sorry which is the use Ur hook and uh just real fast it's just going to return urbe really easy and so we could say urbe is going to be use Ur and if we log it of course when we
enter the page it's going to set the value to urbe you need to of course this is really easy but if for example in every page we wanted to uh fetch the data of the user we can basically copy this use effect over here we can copy it and we can put it inside our uh our userb hook and here we can say as we did before the first name and the set first name use State
string and in this case we can return let's let's do it like this so we have the we're going to create also the set last name this is just to show you and here we can say set last name to the data last name and we're going to return this stuff so basically what I did here is I've copied the use effect before and the use effect works in the same way so whenever this hook this this us gets mounted it's going to call my API it's going to retrieve the user data and
it's going to return an object containing the first name and the last name and so I can basically instead of doing all of this stuff over here I can just simply delete all of these and say user is going to be usur and instead of showing here let's remove this one because it's no longer needed I can get the user. first name from my UK and so if I go on the page as you can see it works the same way as before so in what in whatever component I want to do this it's going to work so I can take this and put it inside the
inside the homepage remember the use client directive and I can show again and the as you can see my name gets printed here because this is the header component as you can see this is the div. header well instead this is the the homepage and actually I'm basically not reduplicating the the code so it's always the same code yeah but you the set functions the the set functions are inside the hook as you can see so basically I I move them inside the hook so instead of
yeah fun where think must what must be implemented sorry and function they are here ah okay and uh so yeah creating hooks is not something that you do like it depends of course if you are creating
libraries and if you want to speed up the development and also have cleaner code is something that you do but the most important thing is that um Li liaries of hooks are really important and they are basically the core of uh the web threee development and also the web two one but mainly in the web three development and we're going to see them uh tomorrow how by using uh the logic of hooks we can uh interact with smart contracts and U so on and so forth so uh we saw these Hooks and something else that I wanted to show
you we show the use State use effect let me see if there's something here that we can H yeah let's talk a little bit about about this stuff so this Library over here that we're going to use tomorrow which actually is the base of uh what we're going to use tomorrow is built on so it's called wagi wagm it's actually a slang in the web three that means we are going to make it
which is really funny and um so basically wagi it's a it's a library that contains only hooks so this is what I was saying before so these are these are all hooks that you can use inside your application uh in your dab to basically uh connect users to send a to what is it to send a transaction to sign a message to read data from a smart contract to what is it to write contact which means to interact with um with a solidity function on a
smart contract and this Library I think right now has become the the standard thankfully and um I want to see real fast have you deployed yesterday the smart contract with fabrio have you deployed it somewhere has Fabrizio put something on his Library aski Network okay no I wanted to see if he if he did put
the the address of the of the deployed smart contract somewhere because I wanted to show you real fast the hooks but I don't think we can show them but we can we can do something I'm going to deploy it real fast which is this one no this is a multi send is this final assignment here right the contract that you that you
deployed yes okay so just real fast I'm going [Music] to or maybe we can get another one like this one let's change [Music] to I don't think it's sepolia base sepolia do I have any test net
here okay should be on aleski aleski aleski why it's not whatever it's not read in my wallet whatever we're going to do it tomorrow I'm going to deploy it on my own and then then you're all going to use the same contract but so just to give you a little glance of what we are going to see
tomorrow so tomorrow we are going to use this scaold D here so as I was saying before scaffold it is based on nextjs it uses wagi it uses it uses typescript and you should have used hopefully OT no you didn't use art that okay whatever and uses this framework for solidity project which is called ardat that you do not need to see tomorrow but basically we are going to copy the solidity code that you developed yesterday and we are going to put it inside uh scaffold it so what's really cool about scaffold it is that uh when you develop smart contracts
you can basically um it basically generates you a simple front end where you can debug your smart contracts so as you can see from the from the screen over here this is all generated uh dynamically based on uh the code that that you've written so tomorrow here you will see the what is it for example you will see the vote function the and the extract winner which is actually the only two functions that you have and uh it's really powerful and um because it already comes with a lot of stuff already done and already customized on top of your smart
contracts what does that mean is that from a developer point of view uh when you will I don't know you will want to call the vote function your um your ID in the case you're using for example vs code or cours or or whatever any smart ID you're going to have uh automatic autoc completion of all the functions that your smart contract have all the return values and so on and so forth and also you will have some useful components which will be already built that you will not need to basically develop from scratch like for example the the address input component showing here or the balance which already reads
the balance of the current connected address from from the chain and so on and so forth and they will be at the aaton right Lon Beetle Guild will be a Dion right yeah they will have also a bounty for using scaffold it so that's why I I I wanted to show it to you so that you can use it and um so basically a bounty is um in in an aazan in the case for example for ethro a bounty is uh a prize that a sponsor uh gives to the to the
first winner or to the top three that they decide that have won for example their their category and uh it's based on most of the times on a on a task or a challenge that you need to do or it can be easy as for example the best three projects that I've used scaffold it are going to get uh $1,000 each for example so that's a bounty basically and they are offering 3K and I don't know how the structure I don't remember maybe demon knows but and uh they they are are going to give money to who uses scaffold it and um yeah I think that they will also will
be there in person so hopefully Kevin is not going to be here Austin Eda okay so you can come to me at the aaton and no worries Basics prob a session around how what to expect what you should be doing you should be doing it some best practices other stuff so that you get prepared you know what's
a b what's a workshop are for example so tomor willover everything that in theend yeah so I'm thinking what we can do right now because we saw this stuff yeah yeah it let's how much time do do we have left 40 minutes okay 40 minutes we can I think if it makes sense to create the scuffle this right
now oops the audio gu is going to hate me so yeah didn't want to do it but whatever so if you go on scaffold d. you have the same command over here uh like similar to the one that we executed before for for a next stop uh we're going to use the the scaffolded command to create a new scaffolded project so we it should be npx create latest of course you need to install the package thankfully he was
out and um let's call it ampus urbe campus when it's going to ask you what solidity framework do you want to use select OT what's your people sayure it depends so Foundry Foundry it's way more complicated so it's not for beginners and I would say not even intermediate because it's it's everything is solidity based even tests so you would need to write tests for solidity it supports a lot of complex stuff like fuding which means like
shaking things up and so it depends like I prefer hard that especially in in a context of anadon because it's faster to use and F instead since you need to write everything in solidity it's it it can become messy see but yeah so in my opinion hard that but I'm I always use hard that by the way EAS yeah yeah yeah it's way way way way easier I thought that that you used it in the in the last two days but like taking a look at the at the fabri repo I don't see any any ah yeah I see are that here so
maybe used something about that that so I don't know but whatever well at this point I can show it to you real fast so so once you have it installed you have it tells you that it requires Yarn yeah so install yarn so to install you can execute this command here in case it only uses yarn you can use npm in install D- Global yarn and it's going so yarn it's basically
another mpm package manager it's faster of course it's not the best but it's faster than npm and someone prefers it to use it so and it's kind of fun that you need to use mpm to install another mpm packager but whatever so if it requires you to use yarn you can install it from here so mpm install D- Global yarn um I have a question about node yeah um I manually installed mod like two years ago on my computer and now I try to update it and using
mm um I have to remove version because I actually have the 202 version of node if I execute the export andm blah blah blah yeah uh but if I put that in my CSH file um I have a really really lazy loing every time I open up a new terminal so you you have installed MVM in the past like node in the past and then you installed MVM basically yeah so it's better that I remove everything and yeah it's always the best solution
is to remove everything and restart from NV the good old reboot but yeah I would I would do it this way because I don't know what the problem actually works if I every time export uh because either way I myal recogniz the the okay no yeah my opinion you should uninstall it and reinstall it because it's because otherwise I already no it's kind of slow so so just I while you install stuff I would just tell you a little bit about
the the structure of of a scaffold it so a scaffold uh scaffold it has these two packages inside the one package is one package is the next JS app and the second packages is the. Project as I was saying before AR that is um uh a framework for developing and testing and deploying smart contracts so here you can write your solidity stuff in this case this is a greting fun uh greeting contract and uh you can create some deployment Scripts in typescript also you can have some custom scripts over here but the
the the idea of this combination between Ard that and and and the nextjs that they have done with scaffold it is what I was saying before of basically they will generate some typescript typings that we' have seen earlier today and based on these typesc typings you will have some custom Hooks and Custom Auto completion and so on that will speed up the development of uh your project so uh just to it should be around I don't remember where it is I think that we let's go real fast to oops to the GitHub I scaffold
this no not this one this one so when you install scaffold this like in this case we use the we use the git clone like we didn't use the git clone we use the command so we do not need to install anything what it's cool about Ard that is that instead of passing through remix which I really hate if you want my opinion I think remix is one of the worst tools that the humans have ever created and um inard that instead what it's really cool is that you can use a command which actually spawn a local chain this is a local chain running on
on my PC of course nothing is happening it's not going to consume my Hardware it's not going to mine anything strange and uh this is local and it's used for testing okay so you do not need to start importing stuff getting your tokens from somewhere and so on and so forth and uh I'm going to show it to you right now with the current smart contract that is that is the default one for scaffold it but tomorrow we are going to use the smart contact that you developed in the in the last days so once you have the the chain running as you can see these are all the accounts the various private
Keys you can actually connect to this chain using metamask if you want if you put this URL and you put this private key you will have 10,000 e but you will not be rich because it's all fake so uh if you run yarn deploy what it does it's going to compile the smart contact and it's going to deploy it as you can see these are all the various uh codes that have been done to my local blockchain it's going to receive the accounts blah blah blah it's going to deploy the smart contract so this is the the the address of the smart contract that has been deployed and uh in this case the deployment script also
calls the greting functions but whatever so as you can see using G that basically I was able to uh run a local chain and then deploy it what it's really cool about Ard that is that if you go in the Ard that config file you actually have all the chains over here so if I were to use my real private key and which is is this deployer private key if I was to use this PIV my private key and I was to choose for example mainnet I would actually deploy the smart contract on mainnet or on seol your base or whatever chain so it's also faster this way because you can just
type y deploy I think it's Das Das Network and the name of the network and it's going to actually deploy the smart contract over there using my funds so I do prefer it rather than remix it takes a little bit of setup and time and understanding of how how that works but we will see it real fast tomorrow so as I said we deployed this SM contract and these are some logs and whatever and as you can see once uh scaffold it uh finishes with the with the deployment of a of a of a smart contact is going to create this contract definition file it's going to create it inside our own
our um nextjs app if we follow this path so nextjs contracts deployed contracts so deployed contracts as you can see we have this huge strange object which is actually composed of the chain ID so the number that identifies the current chain that we are working on in this case it's a local chain and that's this 31,33 7 ID your contract is just the name of the contract really easy and then we have the uh the address where the contract is
deployed which you can see is the same matching here 0x 5f and blah blah blah and then you have the ABI which I think that fabrio told you about yesterday so the ABI just test the specification of all the various functions so once this was was done we can run the last command so we've done yarn chain in the in one terminal window then we have done yarn deploy so we deployed the default smart contract and then we are going to run yarn start so Yen start what it does it's basically it starts the nextjs app so as you can see we are calling nextjs and it's going to create
it on like open it on the local L 3000 it's going to load because it's a little bit slower than of course an empty nextjs project but when it compiles for the first time then the other times are faster so so this is [Music] the this is the default page for scaffold this and [Music] um as you can see it gets already already my connected address in this case it's uh it's connected it's getting it from the wrong Network because right now
I'm on ethereum with my wallet but if I click on switch to add that it's going to add I'm I'm using rabby which is another browser wallet because I don't use metamask anymore but it works the same with metamask so if I click I just clicked on switch to hard that and basically what it prompted me is was to I don't know where it went but if I click on switch on Ard that it's going to add the Ard that chain to my to my wallet and then as you can see now I have of course I have zero e because I'm not connected with my um with any of these private Keys over here but what's really cool about scaffold it is that I can grab funds
from a faet so I just click over there and I get one e every time I click here since I'm in the in the local AR chain I'm just basically spawning money as you can see so it's really useful for for you to test stuff you just click here you get the money and then you do whatever you want so by then the so the on page is basically nothing as you can see it's just some widgets and stuff but what it's really cool for you is the debug debug contracts page in this case we only have one contract but as you can see we have like the this page is divided in no this is really ugly so let's do it this
way so this page is divided in the let's not use this you have of course the name of the contract the address where it was deployed you have the balance of the contract and the network of course then what you see over here are the um are the public variables of the contract so if we go real fast to the to the contract we have owner we have owner greeting premium total counter which are greeting owner premium and and total counter and then we have also this mapping which is public and a mapping is called like a function in solidity so in this case we have this read
function and then we have two right functions which are the set greeting and the withdraw this case I'm not the owner so I will not be able to withdraw but if I take my address for example and I put it over here if I click read it's going to say zero so there's no greeting for me nobody greeted me but if I um if I if I want I can set a new greeting so I can call like this is literally calling the smart contract so if I say hello from urbe campus in this case I don't know how this Mar contract works if I need to send something but yeah
let's send just multiply by one of 18 okay hopefully this is going to send one e but whatever so this is calling the set greeting functions function which is this one and the set greeting has the new greeting variable um parameter sorry and then as the value that we need to send and if I click send as you can see this is actually creating a transaction for me this is a transaction directly on my wallet I can sign it and I can confirm it and the transaction has been created and as you can see it automatically updates the data so you have the
greeting that now has become hello from urbe campus uh it's premium because I don't know what this contract does but the total counter now it's it's one and if I call the user greeting counter now my counter is one because I've sent one greeting so you can see how it's a really useful tool for um smart contact debugging and I'm going to show you a little bit more about the smart contact debugging and tomorrow we're going to see how to use the front end hooks that scaffold it offers CU we do not have any time right now now so might as well show you the
debug stuff so it also shows you the transaction receipt directly over here so you can have you can take all this information the ash how much gas it used and so on and this is the withdraw function that I don't think I can use because as you can see I'm not the owner so it directly tells me uh what it's what it's wrong basically so it's really powerful and if we what it's really cool is that if we change something in the smart contract so let's add I you you do not need to do this because it's it's not useful for for you but it I just want to show you
if I add a function called hello that just returns hello word for example okay if I stop this server and I say yeah and deploy now the smart contract has been deployed to a new address it has updated my deployed contracts if I go back to this page when it loads when it loads as you can see the um oops this I want to show you after as
you can see the address has changed right now so there's no longer the old smart contract but there's the new one which is 0x01 and also has the Alo variable in this case because it's a pure function but so it's really in my opinion the best tool to debug smart contracts for all this reason so I just want to update this one and say because I want to I want to show you the last uh debug uh smart contract debug functionality of scaffold it let's call it building Unstoppable urbe campus I'm going to pay
one and what it's really nice is that it comes also with a block Explorer functionality so the block Explorer functionality in this case it's uh connected to the local network but it's useful for debugging purposes because you can see actually what happened so in this case uh what what what's it like 3 minutes ago I've called the set greeting function on the 0xfb that was the old smart contract with this value and then I've deployed another smart contract I called the set greting function and so on so it also has this cool functionality
of um of block Explorer you can also I think filter from from here this case you can see all the transactions that I've done to me these are all the foset transaction so receiving money from the faucet or the uh calling of set greeting in this case I can also filter by smart contract so if I paste over here and click on search I can see all the transactions that have been done this is the bite code and the op code this is really deep but um you you also have events if any logs have been uh events or logs have been emitted by the
smart contracts I don't know if you've seen events okay so where did he went okay so yeah I think that should get the point on why this is better than remix in my opinion but um yeah if you have any question regarding this part of scaffold it because tomorrow we're going to see how to play because playing scaffold it like started in this way it's just a debugging tool but actually since it's such a powerful debugging tool we can uh use the various Hooks and components that are available here in our advantage to actually build a front
end so that's what we're going to do tomorrow basically so if you have any question on the the buug side or ons scaffold it let me know no you also have the price of of e in case you need it and the light mode enabl so the the the switch of the theme you also have a QR code like you get the point it's it's it's a starter for for um for pretty much anything so yeah no
questions I'm going to St at each other for 15 minutes I don't know so yeah what we can do maybe is let me get the the smart contract so that you can see let me remove this stuff for Simplicity I'm going to call call it your contract I'm going to remove as I said there's no need for you to do this it's just we're going to do it tomorrow together just to pass a little bit time together so I just basically changed all the
code and with the with the code of the of the smart contract that you did yesterday yeah and of course if I don't change the values I need to pass a candidate yeah so I can show you real fast how how are that how hard that works so as I said before are the test the contracts code which is solidity and then as the deploy uh scripts which are typescript via the deploy scripts you can deploy as the name say as the name
says the the smart contracts just to make it a little bit prettier for you as you can see it's really easy to deploy a smart contract all this stuff it's standard so that's not really something strange to understand but in this case what we are doing is that we are deploying the your contract which is the name of the of this SM contract in this case from it's the it's the deployer which in a local environment which is the one that we are using right now uh it will be the the first address over here the first account but if you're using your
own private key it's going to be the first address of your private key then the AR arguments that need to be passed to the to the smart contract in this case for your smart contract that you developed yesterday it wants an array of candidates so as the arguments are an array I'm going to pass the address of the deployer as a candidate and then I'm going to also add my address as well and then logit to print whenever it does something and out of mine it's you should always keep it because it basically um sends the transaction and also mines it so you do not need to wait
pretty much anything so now if I do a yarn deploy hopefully it should work if there's no bug in the smart contract yeah okay so we basically deployed the smart contract that you did yesterday and if we go back to the yarn start and to the I'm getting blind but [Music] right now if I go on theb back contracts as you can see everything has changed and this is the Smart contract that you
did you have the this extract winner the owner you have the the candidates you can call the first item of the array the second one this case it's me because I the first one is the owner in this case i' I've passed the same address and then I've sent mine along with it so it's right that it's me and and yeah as you can see I did not change anything in the front end everything it's autogenerated in this case so I think that it's really powerful and also you can as before I can vote basically for myself because why not
as you can see I can send the function and now the winner it's me and I have one vote and so on so it's really easy also [Music] to like to debug smart contracts and what I was saying before about the types generation is that scaffold it offers all these different hooks which are like the main ones that we are going to see tomorrow are the use scaffold contract the use scaffold read contract and the use scaffold right contract because these are the ones that allows us to um interact with a with a given smart contract but of course there are other
that are really useful like uh the watch contract events that basically allows us to um listen to all the events that happen on that given smart contract whenever they are emitted basically basically and uh just to see should be on up so I do not want to go into the mass of this page so we're going to do it real fast on the on the on page is again we're going to go more in detail about this tomorrow but I don't remember what's the return
return but if I use for example use cuffle contract what it's really cool about this zook is that I can pass um I can pass the contract name and as you can see I already get AOC completion of the name of the smart contract that I'm that I'm working on and if I add more I of course would have more smart contracts but in this case it's just one which is called your contract but it will be called Simple voting tomorrow and um what's really powerful is that this for example this use scaffold contract it's going to return the data in this case it's
data and if I log this data I will get the if I go on the home I will get all the data related to my to my smart contact so I get the ABI I get the the these are function to for example simulate transactions I get the address and so on and the same goes with the let me comment this the same goes for the other hooks that I was telling you before for example the right contract as well it's really I know this just gets the the your
contract and this one instead returns the this returns two functions well instead multiple function but it's a right function and a right contract with right contract and right contract as sync we are going to use always the a sync function and what's cool about this function is that [Music] um okay okay and
um we have it so as as I was saying the what's cool is that for example like the autoc completion it's really powerful because it allows you to have to not perform any kind of error which it's the hardest part When developing front end in web 3 and we will see them tomorrow a lot of the issues that a lot of the issues and the problems that may occur When developing um a front end that interacts with the blockchain the first one is is passing
wrong parameters to the smart contract so imagine for example that you develop a a smart contract where people deposit 10,000 no well 10 10,000 of it is a lot but they deposit a lot of money inside of the smart contract okay imagine that when you perform the deposit instead of depositing I don't know one e you type something wrong and you deposit I don't know 10,000 e and stuff like that so of course the user right now has a lot of ways to see what it happens with some more complex wallets but in the past it was not like this so performing some errors like this could end up in you
like could end up in the user maybe losing a lot of money money or interacting with the wrong smart contract or even just calling the wrong function uh sometimes it would cost money and that's not good but so just to show you you get autoc completion for the for the name of the function in this case there's only one right function in our smart contact which is vote and you and you also get autoc completion for the arguments in this case it wants a string because an address is actually just a string so now this is of course not a right address please but um yeah you get the point and the same goes with
the with the use scaffold read contract this should get the contract name which is always your contract and here we can pass the as you can see we have candidates we have extract winner we have owner this case let's get owner because does not have any does not have any arguments and if if I log data if I log data inside my browser you will see undefine undefine undefine until it's going to get the the data which is the owner of the
smart contract and if we go into Deb contracts we can see that it's the same so 0 XF 39 so scaffold it offers us these really cool hooks that uh we can use to interact with the with the smart contact we are developing so we are going to use this kind of hooks tomorrow to build a custom front end on top of what you built yesterday so it's going to be a simple voting G so don't expect anything too complex but we're going to use these Hooks and these functions and yeah if you have any questions please have some questions if not for me you can go in peace and we
will see you tomorrow thank you thank you guys for much [Music]
Related talks
- Web3 Frontend Development, Part 2
- Desarrolla tu Primera App Web3 Full Stack | Curso Cyfrin Updraft en Español
- Crea Tu Primera Cripto Moneda ERC20 Desde Cero | Cyfrin Updraft
- Scaffold ETH 2 - Extensions Development Guide
- Ethereum Dev Onboarding Session
- Building on Ethereum in the AI Era
- Solidity 101 in Turkish: Updraft Study Session, Part 1
- Ethereum Foundation: Web2 dev to Web3 dev with 🏗scaffold-eth with Austin Griffith
- Solidity 101 in Hindi: Updraft Study Session, Part 1
