What is SvelteJS? How fast is SvelteJS?๐Ÿš€๐Ÿš€๐Ÿš€

What is SvelteJS? How fast is SvelteJS?๐Ÿš€๐Ÿš€๐Ÿš€

ยท

4 min read

Svelte is a Javascript tool for creating UI components just like React, Angular, and Vue. But Svelte is different from other traditional frameworks that would ship JavaScript runtime to the browser to make your code work.

Group 2.png

Svelte is a compiler. It converts the declarative code (that we love to write as a developer) into imperative code that works with a native browser. As a result, you will get high-performance code in a small package, and no extra scripts or libraries were ship to production.

Group 8.png

We can use Svelte to control just certain parts or sections of a site, and we can use it to create an entire website which is typically called a single page application. Let's see some of the cool features of Svelte through this article.

Let's get started,

Installation and Folder structure:

You can use the Svelte project initialization command to create a boilerplate for the project. Before initializing a project, please check if you already installed NodeJS on your machine.

npx degit sveltejs/template svelte-app

# or download and extract this .zip file

cd svelte-app

npm install

npm run dev

And if you need more details, please refer to the official document of Svelte.

After the project initialization completed, you will get the project that contains a folder structure like this,

Screenshot 2021-05-22 230828.png

We can create components in .svelte files which contain three main parts, a script for your javascript code which can also be typescript. A style tag for your CSS can also use a preprocessor like SCSS and, The main template represented as regular HTML, but it's not an ordinary HTML. It contains all kinds of extra syntax for writing declarative code.

Screenshot 2021-05-22 231814.png

Reactive state and scoped syntax:

If you need a reactive state, define a variable inside the script tag with the let keyword, then reference it dynamically in the HTML using braces.

Screenshot 2021-05-22 233719.png

And every CSS styling inside a single svelte file is scoped, so this means that your styling is not going to compete with other components.

Group 11.png

Data binding and Event binding:

If you are working in a forms module, we can get the value of each input by using the bind keyword. And whenever the user enters some values in the form, the UI will automatically update.

Screenshot 2021-05-22 235858.png

If you want to change the state by listening to an event, we can create a function and bind that function to some of the DOM events.

Screenshot 2021-05-23 000404.png

Conditional logic and loops:

In many places, you'll need to run conditional logic or loops in our template. Instead of breaking our head with JavaScript functions, svelte empowers you with a syntax where you can clearly define an if-else statement or a for-each loop when working with a list.

Screenshot 2021-05-23 001146.png

Cross-component communications:

During cross-component communication, svelte provides multiple different strategies for sharing data between components. To pass data from parent to child, you can use props by adding an export keyword to a variable.

Screenshot 2021-05-23 002351.png

And if you have a ton of props, you can use spread syntax to keep your code looking fit and healthy.

Screenshot 2021-05-23 002656.png

Promises:

Svelte has a great way of handling promises by awaiting promises directly inside the template. In promises generally, three things are happening. The first one is the loading state waiting for a promise to resolve, then you get a value, or maybe you get an error.

Screenshot 2021-05-23 011741.png

Context API and stores:

For more complex component trees, you have a context API like react.

Screenshot 2021-05-23 004332.png

And svelte also has a mechanism called stores which are like observable that can be shared anywhere in the component tree and subscribed to in the template by putting a dollar sign in front of the variables.

Screenshot 2021-05-23 005006.png

After building a complete project, you can use the svelte compiler to convert your code into Vanilla JavaScript. By using the command,

npm run build

# it will create a build folder that contains js bundles and CSS bundles.

If you are developing a fully functional web application, you can use Svelte Kit, which comes with Server-side rendering, routing, and code-splitting. And If you are already using Svelte in your project please post your thought about svelte in the comments ๐Ÿ‘.

If you like the content, please share this article with your friends.

Thanks for reading!

Happy coding.

ย