Skip to content

Ability to pass primitive reactive entity by reference from the template #15378

Closed as not planned
@webbby

Description

Describe the problem

Currently code like this

<script>
  let counter = $state(0);

  function increment(e,entity) {
    entity++;
  }
</script>

<div>{counter}</div>
<button onclick={() => { increment(event,counter) } }>Increment</button>

passes just the value without the reactivity.

Describe the proposed solution

I believe the ability to pass the reactivity reference enables a good practice to be able to decouple methods and state which improves re-usability and dependency and gives more freedom.

Probably something like

<script>
  import { ref } from 'svelte/reactive';

  let counter = $state(0);

  function increment(e,entity) {
    // here entity should be the  " let counter " reactive state and it should increment the state value
    entity++;
  }
</script>

<div>{counter}</div>
<button onclick={ () => { increment(event, ref(counter)) } }>Increment</button>

or whatever solution is suitable that may bring the feature available and keep default behavior unaffected

Importance

i cannot use svelte without it

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions