Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proxys inside $state don't work #15175

Open
GuillaumeDerval opened this issue Feb 1, 2025 · 2 comments
Open

Proxys inside $state don't work #15175

GuillaumeDerval opened this issue Feb 1, 2025 · 2 comments

Comments

@GuillaumeDerval
Copy link

GuillaumeDerval commented Feb 1, 2025

Describe the bug

Proxys are somehow ignored once they are inside a $state.

This occurs both on Proxies being directly $stated (as shown below) but also on proxies that are deeply nested somewhere inside an object (a more useful usecase :-))

Reproduction

Playground link: https://svelte.dev/playground/78baf2231a9647d09b52743fe53b8437?version=5.19.6.

<script>
	let thing = {
		entry: "one"
	}

	let proxyThing = new Proxy(thing, {
		set(target, prop, value) {
				console.log("set", prop, value);
				return Reflect.set(...arguments);
		}
	})

	proxyThing.entry = "two"
	
	let stateProxyThing = $state(proxyThing)

	stateProxyThing.entry = "three";
</script>

<input type=text bind:value={stateProxyThing.entry}/>

Current behavior:

  • Log (in console) set entry two
  • "three" appears in the input field

Expected:

  • Log (in console) set entry two
  • Log set entry three
  • "three" appears in the input field
  • When the input field is modified set entry X appears in the console, with X the new content of the field

Logs

System Info

System:
    OS: macOS 15.2
    CPU: (10) arm64 Apple M1 Pro
    Memory: 102.95 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 23.6.1 - /opt/homebrew/bin/node
    npm: 10.9.2 - /opt/homebrew/bin/npm
  Browsers:
    Chrome: 132.0.6834.160
    Safari: 18.2
  npmPackages:
    svelte: ^5.7 => 5.19.6

Severity

annoyance

@7nik
Copy link
Contributor

7nik commented Feb 1, 2025

That's because $state lazily copies the value. Instead, you should proxify the $state.

Talking about the second case, it's impossible to detect whether the object is proxied. So I see the only way - also $statify the value because currently stateful state isn't copied.

@GuillaumeDerval
Copy link
Author

Thanks. Are these two behaviors ($state lazyly copying and $state not copying $statefied objects) documented somewhere?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants