Check out a free preview of the full Rapid Application Development with Code Generation course

The "Code Snippets Demo" Lesson is part of the full, Rapid Application Development with Code Generation course featured in this preview video. Here's what you'd learn in this lesson:

Lukas demonstrates generating a props schema with dynamic variables to assist with quickly building forms while avoiding copy and pasting code.


Transcript from the "Code Snippets Demo" Lesson

>> If you noticed, I wrote one property object, and then I basically copied it down, went through, and I replaced it. And so, in my opinion, code snippets are the Hello World of code generation. And so, what I wanna do is I am going to attempt to actually convert what I've done, the props, into a code snippet that I can reuse.

And so, this to me is, I think, most people, they start to use code snippet. Certainly, I know there's a ton of libraries of like Angular snippets and React snippets and Ionic snippets. And what you're going to see is what these individuals are doing. They're basically taking this approach and turning it into a package that then gets installed as a plugin.

So, you'll notice here that, again, I went through and I just copied this and I pasted it in three more times, and went through and changed the stuff that I needed to. Well, what you can do in VS Code is that you can create a code snippet. And so, in the case of StackBlitz, you can go into your snippets here and just, So there's no surprise here.

I've already done this, but I'm going to do it again, so you can see how this works. And so this is just a JSON file, and we're going to create a new snippet. And so we're going to really generate a props schema. Cuz the one thing that I did notice is, it's really tedious, if I have an interface, to have to convert it into the schema over, and over, and over.

I feel like there should be a way to streamline this, at least, a little bit. So, from here, We'll just add in our scope. And I'm just doing this all by hand, I mean, how far do you wanna take this? We could write a code generator. And I'm gonna go fem, for Frontend Masters, props, and now we're going to do our body.

Now, this is where things get a little funny, and I think where snippets can be, at least setting them up, can become a bit tedious. But notice, speaking of molds, I'm taking an instance of the real thing, and this is what I'm starting with. And so, from here, what I'm gonna do, is I'm just going to do a vertical selection here, and I'm going to go, Double quote, Double quote.

And I'll go back here, add this in, oops, the one thing I should have done, Is I forgot to put in a comma. So we go comma here, and then we ask ourselves, so, this is really important. What do we need to change? Well, we need to change this, and so what we're gonna do is, we're gonna put a 1 here.

Let's assume for now everything is an input, but we'll put a 2 here. We'll put a 3 here, so, there's three things basically I wanna change. And just to round things off, then I'm going to put a 0. And I actually don't think, well, now I need to make a decision.

Do I put labels on these or not? I'm gonna just go with not. So, I think I can just take these off and hopefully this works. But, notice, I pasted in a real Props object. And then I went through, I pulled out the pieces that I wanted to change, or make dynamic, and I replaced them with something else.

So, this is what I'm talking about when you make a mold, it looks like the real thing, but there's some additional tooling so that you can accommodate a workflow. So let me save this, and let's go back and let's see if this worked. fem-props, ooh, and, for key, let's go with notes.

Notes, add in some notes, and I'll tab out, and, ta-da. I forgot something. Let me go here, fem-props, and let's go with miscellaneous because I am running out of creativity. Enter something random, tab, and then what we can do is just make these not required. So, I'm gonna just cut these out, and there we have it.

So, this may have seemed like a very simple thing, but what you have seen is a very kind of, is a microcosm. Is this idea of, see the real thing, pick it up, capture it, pull out the things that you wanna change, immortalize it, cast it in plaster, and then use it later.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now