Svelte

Passing parameters to Svelte function

In Svelte we can define functions that are going to be called when an event is triggered. For example, to call holaMundo on the click of an element we can indicate it as on:click={holaMundo}

Sometimes it is necessary to pass parameters to that function inside a Svelte component, but we cannot indicate it like this:

on:click={holaMundo(parameter)}

Since that will execute the function as soon as the component is loaded, and won’t wait for the click.

In this post I will show you how to pass arguments to functions in Svelte without calling them automatically.

Pass parameters to function in Svelte

If you want the quick fix, check out the following. To pass arguments, define an anonymous function and within that function call the real function by passing the arguments. This way:

on:click={()=>{holaMundo(parameter)}}

Let’s separate things:

The definition of the function looks like this: ()=>{holaMundo(parameter)}

And we are indicating that function within the on:click={}

We can also pass the event and the DOM element itself, but I’ll explain that in another section later.

Understanding how Svelte works

In this and other frameworks, the function to be called in the event must be indicated, not executed. We must give it something that evaluates to a function or is basically of type function.

That is why if we have a function called holaMundo, we must indicate it like this:

on:click={holaMundo}

It is also valid (but will not work as expected) to use parentheses and call the function, like so:

on:click={holaMundo()}

But in that case, Svelte would call the function and expect holaMundo to return a function. Yes: it would expect that when you call the function, it would return another function that would be executed on click.

Also, that function (holaMundo) would be called when the component is mounted.

Passing the event and the DOM element

On several occasions it is necessary to get the DOM element where the event was fired as well as the event. In that case we can do it like this:

on:change={(event) => {
    onEstadoInputCambiado(event);
}}

Note: here instead of click I am listening to the change event; Remember that it is only for example.

The important thing is that we are passing the event, and then inside the function we can access it as well as the DOM element:

const onEstadoInputCambiado = async (event) => {
    const propioElemento = event.target;
};

We have the event in event and the DOM element in event.target.

Passing other parameters

Finally, let’s see how to pass our own parameters, beyond the event and HTML element.

I have a set of input checkbox elements repeated with each. When the state is changed I want to call a function and pass the each element to it, which in this case would be elemento.terminado.

I do it like this:

{#each nota.elementos as elemento, indice}
    <div class="flex items-center">
        <input
            type="checkbox"
            on:change={(event) => {
                onEstadoInputCambiado(event, elemento.terminado);
            }}
        />
    </div>
{/each}

In short

If the function has no parameters, execute it like this:

on:click={holaMundo}

If you want to pass parameters:

on:click={()=>{holaMundo("parameter 1", "parameter 2")}}

If you want to pass the event and arguments:

on:click={(event)=>{holaMundo(event, "parameter 1", "parameter 2")}}


I am available for hiring if you need help! I can help you with your project or homework feel free to contact me.
If you liked the post, show your appreciation by sharing it, or making a donation

parzibyte

Freelancer programmer ready to work with you. Web, mobile and desktop applications. PHP, Java, Go, Python, JavaScript, Kotlin and more :) https://parzibyte.me/

Entradas recientes

Receipt designer for thermal printers – Free and open source

In the last months I have been working on a ticket designer to print on…

11 months hace

JavaScript: store and read files with the Origin Private File System

In this post you will learn how to use the Origin Private File System with…

1 year hace

JavaScript: download file with fetch

In this post you will learn how to download a file in the background using…

1 year hace

SQLite3 with vanilla JavaScript and OPFS – Hello world

In this post I will show you how to use SQLite3 directly in the web…

1 year hace

Python Thermal Printing: A Comprehensive Guide for Printing on Thermal Printers

In this tutorial, we'll explore how to effortlessly print receipts, invoices, and tickets on a…

1 year hace

Image printing on Thermal printer

When printing receipts on thermal printers (ESC POS) sometimes it is needed to print images…

1 year hace

Esta web usa cookies.