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

Store not resubscribed when changed (as prop in component) #3715

Closed
theonlypwner opened this issue Oct 16, 2019 · 2 comments
Closed

Store not resubscribed when changed (as prop in component) #3715

theonlypwner opened this issue Oct 16, 2019 · 2 comments
Labels

Comments

@theonlypwner
Copy link

Describe the bug
When a prop (whose value is a store) changes, Svelte does not unsubscribe from the old store and subscribe to the new store.

This seems related to #2014 and #2435, which were previously fixed.

Logs
See below.

To Reproduce
With local variables, it works as expected: https://svelte.dev/repl/f8dca40374cd499b9b2df84a900d908b?version=3.12.1

s1 subscribe
(swap)
s1 unsubscribe
s2 subscribe
(swap)
s2 unsubscribe
s1 subscribe

With a component prop, it does not: https://svelte.dev/repl/fa8f0e0b78f743e5b1410c87885cf277?version=3.12.1.

s1 subscribe
s1 subscribe
(swap)
s1 unsubscribe
s2 subscribe

If <h1>outer $s = {$s}</h1> is commented out, no subscribe/unsubscribe occurs when the button is clicked.

Extra unsubscribe statements appear in the console when editing the code when the old component is unmounted.

Expected behavior
The component should unsubscribe from the old store and subscribe to the new store.

Information about your Svelte project:

  • Your browser and the version: Chrome 77.0
  • Your operating system: Windows 10
  • Svelte version: 3.12.1

Severity
It's not a big issue, but the current behavior seems surprising or counter-intuitive.

Additional context
None

@Conduitry
Copy link
Member

Simpler repro:

Main.svelte

<script>
	import Foo from './Foo.svelte';
	import { writable } from 'svelte/store';
	let prop = writable(1);
	setTimeout(() => prop = writable(2), 1000);
</script>

<Foo {prop}/>

Foo.svelte

<script>
	export let prop;
</script>

{$prop}

After one second, this should switch from displaying 1 to 2, but does not. The $set handler

	$$self.$set = $$props => {
		if ('prop' in $$props) $$invalidate('prop', prop = $$props.prop);
	};

needs to also resubscribe to the store, like any assignment to prop would.

@Conduitry Conduitry added the bug label Oct 16, 2019
@Conduitry
Copy link
Member

Duplicate of #3662.

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

No branches or pull requests

2 participants