What is SvelteJS? How fast is SvelteJS???????

What is SvelteJS? How fast is SvelteJS???????

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. 

No alt text provided for this image

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.

No alt text provided for this image

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,

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

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

No alt text provided for this image

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. 

No alt text provided for this image

Context API and stores:

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

No alt text provided for this image

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.

No alt text provided for this image

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.

要查看或添加评论,请登录

Prasath Ravichandran的更多文章

  • display: flow-root, A nice little alternative for clearfix in CSS.

    display: flow-root, A nice little alternative for clearfix in CSS.

    There are numerous display properties in CSS, but have you heard of flow-root??? Here are the details, In CSS…

  • React native Reanimated - Quick Look??????

    React native Reanimated - Quick Look??????

    React Native, a popular JavaScript-based UI software framework that allows you to build natively rendering mobile apps…

    2 条评论
  • Scoped storage in Android 11 - React native ??????

    Scoped storage in Android 11 - React native ??????

    From November 2021, the google play console forced the developers to deploy the Android app, targeting API 30(Android…

    3 条评论
  • JS [De,structur,ing] assignment ????

    JS [De,structur,ing] assignment ????

    The destructuring assignment is a javascript expression that takes values from arrays or properties from objects and…

  • Async Await try-catch hell ??????

    Async Await try-catch hell ??????

    There are many new features in JavaScript, which were helpful for the developer in day-to-day programming. One of them…

  • CSS is too hard(myth) ???

    CSS is too hard(myth) ???

    CSS stands for Cascading style sheet, and it was awesome, ever since it came out in the year 1996 when Netscape was the…

  • An VS Code extension that would replace Postman!!!

    An VS Code extension that would replace Postman!!!

    I have been working in a Postman for testing HTTP requests(REST APIs), but I just found one VS Code extension that…

社区洞察

其他会员也浏览了