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

Svelte fails when modifying nested writable stores #4495

Closed
milkbump opened this issue Mar 2, 2020 · 2 comments
Closed

Svelte fails when modifying nested writable stores #4495

milkbump opened this issue Mar 2, 2020 · 2 comments
Milestone

Comments

@milkbump
Copy link

milkbump commented Mar 2, 2020

Describe the bug
Svelte chokes when altering the content of a nested store using the $ syntax.

(i.e)

<script>
   import { writable } from "svelte/store";
	
   let store = writable({
	inner: writable("string"),
   });
	
  $: ({ inner } = $store);
  $: inner.set("new string");          // this works 
  //$: $inner = "other new string";    // this doesn't
  $: $inner && ($inner = "other other new string"); // this works!?
</script>

<h1>Hello {$inner}!</h1>

Logs
Results in a Cannot read property 'set' of undefined error.

To Reproduce
REPL repro

Expected behavior
I should be able to assign a value to a nested Svelte store using the $ systax.

Severity
Low severity. It's only a convenience, and probably a rarely used pattern to nest stores.

Additional context
I'm not familiar with Svelte internals but I suppose this may be vaguely related to #4079 and #3637 ??

@pushkine
Copy link
Contributor

pushkine commented Mar 9, 2020

related #4516
$: $inner = "other new string"; is a "one time reactive statement", those run before reactive statements, $inner was still undefined when you attempted to change its value

another fix could be to define your nested store within the static top level block :

let { inner } = $store
$: ({ inner } = $store);

@dummdidumm
Copy link
Member

This is fixed in Svelte 5 through tracking dependencies at runtime and reacting to updates as often as needed.

@dummdidumm dummdidumm added this to the 5.x milestone Nov 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants