Skip to content

Svelte 5: Bug - $state is not properly updated when another $state is modified inside class  #11160

Closed
@mariansimecek

Description

@mariansimecek

Describe the bug

I am trying to create a form validator, and I encountered a bug when it does not change the $state of values on the first keypress inside <CustomInput/>

This is smallest code that reproduce that bug:

<script>
  import CustomInput from './Input.svelte';

  class Form {
    values = $state({name:''});
    errors = $state({});
		
    getProps(name) {
      return {
        oninput: (e) => {
	  this.errors[name] = "something"
        },
        error: this.errors[name],
      };
    }
  }

  let form = new Form();
</script>

<CustomInput  bind:value={form.values.name} {...form.getProps('name')} />
<!-- Input.svelte -->
<script >
  let {value = $bindable(), error, ...rest} = $props();
 
</script>
<input bind:value {...rest} />

Things I found:

  • I don't encouter this bug inside firefox.
  • It works when I move function getProps() outside Form class.

Reproduction

https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAA6VTTWvbQBD9K4-lIBmEfFckQwkEcik55RLloMjjeKm0K3ZHNkXov5fZVWS77qFQdNHsm48382YmddAdeVW8Tco0PalCfR8GlSn-NYjhT9QxqUx5O7pWXkrfOj3wrjaA7gfrGI-jZ9s_m2FkHJztkeTbYOUxPHmojbi3XeM9nqzrMYkNnJpuJI8K3zw3TOk0bx4iQs5Zd4fUXHPEW2s8u7Fl69IJ2mjWTfca082br_wAH7XP1zI3fkulOZIDPolfnB18KoO4SuGIR2cuNmCNlvYKpLRBtRMoMAtfqBjpv0mmd1Solbc98VGbz1pd8szZ5T9EFLiLXl3mC9-VdEeMg8yzgqFzGG268LxptcAEyVYgSZaqYZ7ldlWzHIKkT9p5RntsXNMyOWgPYxmyDntog9i4RIp_bcpr8Yeuaelouz25qlYRiRG1woc2-yIoUU3CeVElF14zpjzPw-uqQiJAspmxXdk9M87W_fQ4az7CNKxP9AejUv-FyY8rz_9lUhuVqd7u9UHTXhXsRpqz9XjCHP7hfLD7ku_6EmTdhVzz0VG6yeJWhWVYtiDPc0eeBZjFeQj8ws3cihmncGk0dCWh0sR9A-_zb84yRwEJBAAA

Logs

No response

System Info

System:
    OS: Linux 6.7 Fedora Linux 39 (Workstation Edition)
    CPU: (8) x64 Intel(R) Core(TM) i5-8300H CPU @ 2.30GHz
    Memory: 5.38 GB / 15.48 GB
    Container: Yes
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.10.0 - ~/.nvm/versions/node/v20.10.0/bin/node
    npm: 10.2.3 - ~/.nvm/versions/node/v20.10.0/bin/npm
  Browsers:
    Chrome: 123.0.6312.86
  npmPackages:
    svelte: 5.0.0-next.102 => 5.0.0-next.102

Severity

annoyance

Metadata

Metadata

Assignees

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