Okay, so let's just quickly go through the agenda here. And I hope everyone has got the link for the workshop material we're going to be working through. There is a link in both the Zoom chat and in the Discord thread. Can I say hi on Discord thread as well? I think it'd be good to just manage it in one place. If you have any questions, then I think you can raise your hand or just ask. Everyone's got their microphone available, and everyone's muted at the moment. So thank you all.
Right, so let's just quickly rattle through the agenda and what we're going to be doing today. So we're going to be building a small blog with a GraphQL backend, and we're going to be using SvelteKit for the front end. This is a good example because it goes through several things which are sort of pertinent to SvelteKit. But we're doing that, we're writing some GraphQL queries in GraphCMS. We'll be querying the data we've written with those queries in Svelte, we'll be retrieving information and displaying it in our project. We're going to take a look at Svelte directives, Svelte routing, and endpoints in Svelte. And then finally, we're going to deploy it to production. So we've all got a nice little project at the end of it.
Okay, so let's talk about Svelte briefly. And when I say Svelte, I really mean SvelteKit. SvelteKit is, I mean, Svelte still has, it's still used in a lot of places, but SvelteKit is like the, like it's going to be the one-stop shop for creating any new project in Svelte with the sort of functionality we're going to be looking at today. So what is Svelte? Svelte's a component framework that allows you to create components for your projects as reusable components, much like all the modern frameworks we've got these days, React, Angular, and Vue. The main difference between Svelte and technologies like React and Vue, let's say, is Svelte's a compiler. So what that means is you write your declarative code in your text editor, and what it produces at the end of that, once it's built through the compiler, is plain JavaScript, HTML, and CSS, which can be used by the browser. So there's no virtual DOM, there's no diffing. Svelte is clever enough to do all of this up front before it's built. And Svelte, I mean, it's relatively new. The V1 was released in late 2016, around November, I think. So what is SvelteKit? As I said, it's going to be the one-stop shop for building Svelte projects. Has modern web development best practices baked in, like root splitting and other things I can't think of right now. You can use HTTP endpoints. We'll come on to those later on. Uses file-based routing. And from giving you that description, you can probably guess it's very similar to Next.js or Nuxt. But it uses Svelte instead of ReactorView. I have heard Rich Harris actually saying that Next.js was, you know, like the inspiration for SvelteKit, and they took a lot of their design direction from Next.js.
Okay. So let's go straight into it. I haven't got many more slides. So I'll just try and go through some of the concepts we'll be covering in the workshop. And then we can start building. So Svelte, without getting too much into this, you can use reactivity pretty much as you would with, let's say, vanilla HTML and CSS and JavaScript, sorry. But I mean, the code here looks very much akin to like HTML file. And in fact, Svelte is like a superset of HTML. So anything you can do in HTML, you can most likely do in Svelte. So with this little snippet here, we're just creating the classic increment count. So the function is declared here, and we're just incrementing count, and then we're using the on click on our button here, and then we're just calling the function. So let me quickly go over here. Okay. Sorry, I thought someone messaged me on the Discord here. Okay. So let's crack on with this. So it's JavaScript, so that function could just be put into an inline function, and even more inline, you could just do it as like an inline function here. Great. We could go through some of these examples in the Svelte REPL later if you like. But for now, let's just go through the rest of the slides, and then we can get on to building.
Okay. So again, here, we're just using the on click inline. So we've got declarations here. So this is the funky bit, which a lot of people who are like first see Svelte for the first time, they're like, ooh, what is that? And that is this dollar and the colon here. So this is valid JavaScript syntax, and it's used as a label in JavaScript. So you could use it as a go to in JavaScript. But in Svelte, because it's a compiler, we can do what we want with it. So in this case, it's a reactive statement. So we're saying that the doubled variable here, so this is both declared in here. So this could be just let doubled, and then we could have doubled equals count times two there. But you can do it inline here. And I'm using another function just to increment the count here. And then again, we're just incrementing it with our on click. Yeah. We could also use if as well. So the curly braces here, this is just to tell Svelte that there's going to be a directive used. In this case, it's going to be if. And then if we create a zero, then we're going to hide this P tag. Otherwise, we're going to show it. So we can conditionally render elements on the page. And unlike with React, it's not hidden. It just doesn't render it to the page. Or there's just no element there for it. And I've got the else statement as well. So we can have something in place of that element. And then we can take a quick look at each. Each directive here. So we've got our speakers object. Sorry, speakers array here with objects in it. And then we're using the each directive to loop over each speaker here. And we're also incrementing this with this I variable. Which can be pretty much anything you want to call it. But the standard is I. And then we're just going to render out an unordered list item with an A tag with the speaker name and a link to the speaker on Twitter here.
One thing we need to cover is the reactive nature of Svelte. This means that if you have push and pop and like array functions, sorry, methods like this in JavaScript, you doing this reactively won't actually give you anything.
Comments