Skip to content

Svelte 5: $state setter this is not the proxy #12499

@brunnerh

Description

@brunnerh

Describe the bug

Setting properties via this in a $state object does not trigger updates since the proxy gets bypassed. this is the raw object.

Should the setter function be called with the proxy as this?
(That is already the case for getters, but maybe there are some reasons not to do this, e.g. performance.)


Relevant code:

// Set the new value before updating any signals so that any listeners get the new value
// @ts-ignore
target[prop] = value;

If the descriptor is checked for a set function, that could be invoked with the receiver. Something along the lines of:

var descriptor = Reflect.getOwnPropertyDescriptor(target, prop);
if (descriptor.set)
  descriptor.set.call(receiver, value);
else
  target[prop] = value;

Reproduction

<script>
  let text = $state({
    value: 'svelte',
    get uppercase() {
      return this.value.toUpperCase()
    },
    set uppercase(v) {
      this.value = v.toLowerCase()
    }
  })
</script>

<input bind:value={text.uppercase} />
<p>{text.value}</p>

REPL

Logs

No response

System Info

REPL

Severity

annoyance

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions