React, Angular and Vue developers would know the packages, node_modules, dependencies they would have to maintain and compile in their app during runtime. This would definitely pull down the speed of the application by a very few milliseconds. Yes, it is very small, but combine every decrease in speed and take a look. We would have lost a couple of seconds, which is crucial. So why don’t we do everything when building the app itself. Instead of compiling at runtime, why don’t we build everything ready before rendering?
That’s where SvelteJS comes in handy. Svelte is not a framework, but rather a compiler. Svelte does not use the virtual DOM, but rather makes use of the real DOM. Svelte takes advantage of this and is lightning fast. So let’s dive a little deeper into creating a simple Todo List using svelte.
In the app, we will take a look at
- How to create a svelte component
- How to pass props between the components
- How to call a function between components
If you are a React developer, you will be familiar with passing props and functions as props or components.
Setting up the Svelte App:
There are 2 ways to get you started with the Svelte template
- Use the REPL
- Use degit
For this app, we will use degit. If you do want to use REPL follow the instructions here .
Use degit:
Degit is a project scaffolding tool. Open your terminal and do the following to create your svelte project.
Let’s now take a look at the project structure.
It is quite similar to a React project. If you see the src directory, you will find that there is a main.js and App.svelte. Imagine the main.js like your index.js which is going to interact with the DOM by compiling everything together. The ‘.svelte’ files are separate components that have its own HTML, CSS and js code. Let’s take a look at the default files
App.svelte
It is just a simple component with HTML, CSS, and js. The good thing about svelte components is that the styles remain inside the component block level. Meaning, adding style to the h1 tag in App.svelte will not be reflecting on any h1 tags used outside of App.svelte. Like Angular and React, the variables are used in the same way.
main.js
Here is where svelte compiles and builds your app. It imports the component App and creates a new element inside the real DOM body with the props name.
Now let’s see our page live
Rendered so fast! didn’t it?
Programmer: npm ru…. Svelte: Done!
This is what’s cool about Svelte. After you have made changes to your code, hit save, check your bundle.js under public and you will be able to find all your components, classes, texts declared everything compiled and ready, just waiting to be rendered.
Now let’s change things up a bit for the todo-list. We will split our app into 4 different svelte components.
- App.svelte
- Sidebar.svelte
- Todo.svelte
- TodoView.svelte
- main.js
Todo List App flow
Let’s take a look at them individually.
main.js
We don’t need to pass any props to the App component, for now, so ignoring the props.
Sidebar.js
A very simple todo list sidebar. We will just create an array to store the todos and render the todos as a different component.
Todo.js
We will just render the new Todo as the received props. We will also add a finish button to pop that element out of the array.
TodoView.js
This is just an additional feature to View the props into another component. Like Sidebar to App and then App to TodoView.
App.svelte
Finally let’s see out app in action.
And we’ve done it
Programmer: We won…..!