Skip to content

Svelte 5: Pass bind:value or function to snippet #15847

Answered by brunnerh
artttimiron asked this question in Q&A
Discussion options

You must be logged in to vote
  1. You cannot bind to primitive values, it would be like reassigning a regular function argument: It has no effect on whatever was passed into the function.
    You can either pass in a stateful object and bind to one of its properties or use function binding syntax, e.g.
    {#snippet withdraw(
        getParam: () => (string | undefined),
        setParam: (v: string | undefined) => void
    )}
      <div class="mt-1">
        <WithdrawOptions bind:testMe={getParam, setParam} />
      </div>
    {/snippet}
    Function binding Playground example
    Object property binding Playground example
  2. You can pass functions around and into the component like any other property. E.g.
    {#snippet withdraw(onchange: () => any)}
      <div class="mt-1">…

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@artttimiron
Comment options

@brunnerh
Comment options

Answer selected by artttimiron
@artttimiron
Comment options

@brunnerh
Comment options

@brunnerh
Comment options

@artttimiron
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants