The JavaScript Framework you’ve been missing out on

Safe
6 min readJan 21, 2023

--

Hand held logo with blurry background

Svelte

Svelte is a JavaScript framework for building user interfaces. It was created by Rich Harris and was first released in 2016. It is a lightweight framework that focuses on providing a smooth developer experience and fast performance.

One of the key features of Svelte is that it does not use a virtual DOM, which is a common feature in other JavaScript frameworks. Instead, Svelte compiles components at build time, which allows for updates to be performed directly on the actual DOM. This can result in faster performance and more efficient use of system resources.

Svelte also uses a template syntax that is similar to other popular frameworks but with a few key differences. For example, Svelte uses a special syntax for handling events, and it has built-in support for two-way data binding.

Svelte also provides a rich set of features, such as animations, transitions, and a powerful set of directives. It supports a variety of use cases, from small web pages to large applications.

Svelte also has a growing ecosystem, with a large number of libraries and plugins available, such as Sapper (a framework for building web applications) and Svelte Material UI (a library for building Material Design interfaces).

Svelte differs from other JavaScript frameworks such as React, Angular, and Vue.js in its approach to building user interfaces.

Svelte focuses on providing a simple and efficient developer experience while still providing a set of powerful features to build applications.

Step-by-step guide on how to install and create a new Svelte project.

You will need to have Node.js and npm (the Node Package Manager) installed on your computer.

Check this out if you don’t have node installed.

Once you have those tools set up, you can use the following steps to create a new Svelte project:

  1. Create a new directory for your project: Use the command line to create a new directory for your Svelte project.

mkdir my-svelte-project

2. Navigate to the project directory: Use the cd command to navigate to the directory you just created.

cd my-svelte-project

3. Create a new Svelte project: Use npm to create a new Svelte project by running the following command:

npm create svelte@latest my-svelte-project

This will create a new Svelte project in the directory you just created.

4. Navigate into the new project directory and run

npm install

to install the necessary dependencies.

5. Once the dependencies are installed, you can start the development server by running

npm run dev.

6. This will start a local development server and open a browser window with your Svelte application.

You can start building your application by creating new .svelte files in the src directory and importing them into your main application file.

Svelte component

A Svelte component is a combination of HTML, CSS, and JavaScript that represents a reusable piece of the user interface. Each Svelte component is defined in its own file with a .svelte extension.

The basic structure of a Svelte component looks like this:

<!-- Component's HTML -->
<script>
// Component's JavaScript
</script>
<style>
/* Component's CSS */
</style>

Here’s an example of a simple Svelte component that displays a message:

<h1>Hello, {name}!</h1>

<script>
export let name;
</script>

In this example, the component has a single variablename, which is passed in as an exported variable, svelte uses the export keyword to mark a variable declaration as a property or prop, which means it becomes accessible to consumers of the component.

The component uses the curly braces {} to interpolate the value of name into the h1 element.

When creating a new Svelte component, you can define the component’s HTML, CSS, and JavaScript in the same file, and import it wherever you need to use it in your application.

Svelte also provides a set of directives that can be used to add functionality to your components. Directives are special attributes that begin with a `$` symbol. For example, the on:event directive is used to handle events in Svelte components.

Two-way data binding and event handling on Svelte

To handle data in a Svelte component, you can define variables in the component’s JavaScript section and use them in the component’s HTML template.

<script>
let name = 'Svelte';
</script>
<h1>Hello, {name}!</h1>

The component has a single variable, a name, which is used to display a message in the component’s template.

Svelte also supports two-way data binding, which allows you to bind a variable to a form input element so that any changes made to the input element are automatically reflected in the variable and vice versa. To bind a variable to a form input element, you can use the bind:value directive

The component has a single variable, a name, which is used to display a message in the component’s template.

Svelte also supports two-way data binding, which allows you to bind a variable to a form input element so that any changes made to the input element are automatically reflected in the variable and vice versa. To bind a variable to a form input element, you can use the bind:value directive.

<script>
let name = 'Svelte';
</script>
<input type="text" bind:value={name}>
<h1>Hello, {name}!</h1>

To handle events in Svelte, you can use the on:event directive to attach an event listener to an element. For example, to handle a button click event:

<button on:click={handleClick}>Click me</button>
<script>
function handleClick() {
alert('Button clicked!');
}
</script>

It’s also possible to access the event object and other properties by passing it as an argument to the function like this:

<button on:click={(event) => handleClick(event)}>Click me</button>
<script>
function handleClick(event) {
console.log(event.target)
alert('Button clicked!');
}
</script>

React vs Svelte

React and Svelte are both popular JavaScript frameworks for building user interfaces, but they have some key differences in terms of how they work and the developer experience they provide.

One major difference is that React uses a virtual DOM, while Svelte compiles components at build time. This means that updates in React are performed on a virtual representation of the DOM, which can result in slower performance compared to Svelte. Svelte, on the other hand, updates the actual DOM directly, which can result in faster performance.

Another difference is that React uses JSX, a syntax extension for JavaScript that allows you to write HTML-like elements in your JavaScript code, while Svelte uses a template syntax that is similar to other popular frameworks.

Example
In React, the state and the component’s lifecycle methods are defined in the JavaScript code:

import React, {useState} from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<>
<h1>{count}</h1>
<button onClick={() => setCount(count + 1)}>Increment</button>
</>
);
}
export default Counter;

In Svelte, the state and the component’s lifecycle are defined in the component’s script:

<h1>{count}</h1>
<button on:click={() => count++}>Increment</button>
<script>
let count = 0;
</script>

React also has a more extensive ecosystem and more resources available, as it’s been around longer and is more widely used. Svelte on the other hand has a simpler and more intuitive API compared to React, meaning the way you interact with the framework and the concepts you need to understand to build an application are less complex and easier to grasp. This can make it quicker to learn and get started with Svelte than React.

In React, you need to understand concepts such as JSX, components, state, and lifecycle methods to build a basic application. In Svelte, on the other hand, you can build a simple application just by creating a .svelte file and writing HTML, JavaScript, and CSS in it.

Svelte uses a reactive programming model, which allows you to react to changes in your data. This means that when a variable’s value changes, the component updates automatically. This eliminates the need to manually manage the component’s state, making it more intuitive and easier to understand than React.

Conclusion

All in all, the smaller learning curve in Svelte means that developers can build web applications with less time and effort than some other frameworks.

JavaScript frameworks are powerful and can be used to build efficient and performant applications. The choice between React and Svelte or any other framework will depend on your specific needs and preferences as a developer.

--

--

Safe
Safe

Written by Safe

An experienced web developer and DevOps engineer learning and experimenting with new technologies, sharing knowledge and helping beginners in the tech community

No responses yet