Before you go, check out these stories!

Hackernoon logoGetting Started With Svelte Framework by@the_rock

Getting Started With Svelte Framework

Author profile picture


Software Developer from monday to friday (Healthcare sector), Game Developer in free time

Installation, Components, Properties and DOM events in a nutshell

Hi rocks, Sasha here. In this article we are going to see some core mechanics of Svelte, so you can easily start making your own webapp with this almost perfect UI library.

Let's start with setup. Open your terminal, go to the folder you want to create project and execute the next commands:

npx svelte3-app
npm run dev

This will create a standard "Hello World" project configured with rollup. The project's structure is pretty standard. The entry point is in

. It only renders App.svelte on body and passes name prop to it.

Unfortunately, you have to add .svelte at every import, but you can fix it easily by updating rollup.config.js: go to

object and add:

extensions: [".svelte"]

Component structure

As you see, a Svelte component incorporates all the necessary inside a single .svelte file. The basic structure is:

    // all js code here

<!-- HTML code here -->

    /* all css here */
    /* css is scoped by default */

Let's create our first component. Name it as you like (with .svelte extension, I called it Page.svelte) and write the above code in there. Import it in App.svelte and put it in your template. At this point, your App.svelte must look like this:

    import Page from "./Page";

    <Page />

I removed style section from it as we don't need it.


The cool thing is that although Page is an empty component, it won't crash your app. It is very useful when you are prototyping the structure of your application, without implementing it yet. You just use this empty components as placeholders, and then put the content you need inside them.

Properties (props)

In order to expose a variable as property you just have to export it as in normal javascript. Let's export a variable named

from our Page and display it:

    export let number;


    /* all css here */
    /* css is scoped by default */

Props in Svelte are set the same way you do in other libraries: add it as attribute in template:

<Page number="5" />

Now, this component will display


Value binding

The last thing we are going to do in this tutorial is to update

by adding a button to increase value in
. Steps are:

  1. Create a variable to store value in (
    letย valueย =ย 0
  2. Pass it as a prop to Page (
  3. Create a function to increase value;
  4. Add a button that calls increase function(
    <buttonย on:click{increaseValue}>Increase</button>

Your App.svelte should look like this:

    import Page from "./Page";

    let value = 0;

    const increaseValue = () => {
        value ++;

    <Page number={value} />
    <button on:click={increaseValue}>Increase</button>

The only new thing here is:

. I think you can figure it out by your self ๐Ÿ˜‰. In Svelte, you can listen to any DOM event using

In the next tutorial, we are going to create a classic Guess a Number game. It will be a bit more complex as we are going to use custom events to communicate with parents and learn some lifecycle hooks.

External links:

  1. degit
  2. npx
  3. Svelte
  4. Rollup
  5. Dom events


Join Hacker Noon

Create your free account to unlock your custom reading experience.