svelte: Svelte 5: can't link reactivity from `$props()`
Describe the bug
This is most apparent when implementing pagination, but when trying to use deeply nested reactivity with page data objects in a svelte kit route (e.g. $state(data.foo)), it breaks the connection to $props().
Accessing directly with dot notation stays in sync with $props(), but does not have reactive state (as expected). The only way to stay in sync with $props() and get reactive state is to use component context to create local reactive state.
Reproduction
Here’s a stack blitz with sveltekit repro. I can only get the expected reactivity by creating $state() in a component based on local primitive values.
And here’s a svelte repro that shows similar behavior, but since the parent App.svelte component can create $state() and pass it into the component, the issue is not as apparent.
https://svelte.dev/playground/6fe00097a3104b11b9922df28b668598?version=5.1.3
Logs
No response
System Info
System:
OS: macOS 14.7
CPU: (10) arm64 Apple M1 Pro
Memory: 83.23 MB / 32.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 22.1.0 - ~/.nvm/versions/node/v22.1.0/bin/node
npm: 10.8.3 - ~/.nvm/versions/node/v22.1.0/bin/npm
bun: 1.1.15 - ~/.bun/bin/bun
Browsers:
Brave Browser: 129.1.70.126
Chrome: 130.0.6723.70
Chrome Canary: 132.0.6803.0
Safari: 18.0.1
npmPackages:
@sveltejs/adapter-node: ^5.2.8 => 5.2.8
@sveltejs/kit: ^2.0.0 => 2.7.2
@sveltejs/vite-plugin-svelte: ^4.0.0 => 4.0.0
svelte: ^5.0.0 => 5.1.0
vite: ^5.0.3 => 5.4.10
Severity
serious, but I can work around it
Additional Information
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 9
- Comments: 17 (10 by maintainers)
One workaround (intended pattern?) we were discussing over in discord was using
$effect()to maintain the$props()connection and nested reactivity.Which makes a certain kind of sense, based on this graf:
We’re syncing local app state with new data from the network, so maybe this isn’t a bug, but the intended behavior.
If so, I think this kind of example should be discussed in the docs, because it feels like a gotcha. I’d be happy to write up something.
IMHO this is a huge regression and breaks every page on our large svelte 4 application as they all use (what we were trained to do) the data directly in our bindings with full reactivity by default.
this now has to have onerous boiler plate additional refactor everywhere as follows
The migrate script just slaps a $bindable() on the data prop which does nothing in regards to reactivity
You can do it with an
$effect, but it feels like the worst of all available options to me.We have this issue anywhere you want to set the value of some
$stateboth from a prop and some other source. There are two possible general solutions. The one that exists today is to use a callback prop. There was also some discussion of introducing something along the lines of$state.link(some discussion in https://github.com/sveltejs/svelte/pull/12943).The case of
datais a bit of a special one because because it’s the SvelteKit framework invoking the component and so users don’t have control over it. We could introduce anonDataUpdatecallback prop, but this doesn’t feel like the nicest API to, which I think speaks to the benefit of something like$state.link. Also, it would make migrations and updating more difficult. The second solution for SvelteKit’sdatawould be if we merge https://github.com/sveltejs/kit/pull/11809 to binddata. This is fairly backwards compatible. I don’t think we should generally encourage two-way data, but this would be a pretty harmless instance of it since there’s only one page at a time accessingdataand we don’t have to worry about multiple stomping on each other. The thing that makes me hesitate is that it feels like we’re teaching the wrong pattern where we need to rely on heavy usage of$bindableand if a$state.linkor similar existed then there’s a good argument that it might be the cleaner solution.thanks! appreciate the response.
I must say that reassigning data svelte-4 style feels a little off, especially since this issue was closed based on the new writable
$derivedfeature. I understand the docs do say that writing to a$derivedvalue is not reactive, but the impression I got from seeing this issue closed was that it would be. I think using$effectremains the most straightforward work around in some sveltekit data-syncing situations.We plan to add it to the docs soon, I also think we should probably abstract this concept in a
$state.linkrune, we are just trying to figure out if it’s worth it considering is doable in userlandI just stumbled upon this issue too and I am surprised that this (https://github.com/sveltejs/svelte/pull/15107#discussion_r1945486274) seems to be the actual right way to do it? Is this really intended? If so, I’m sorry to say it like this, but that’s ugly af…
This feels so unlike svelte tbh and completely kills the DX, which is one of svelte’s main selling points and why I love using svelteKit so far. I just started working on my first project with svelteKit, so I’m not that experienced with svelteKit, which is why it took me longer than I want to admit to find the issue to why my message in
const { message } = datawas not being updated. I was looking everywhere (page.ts,page.server.ts,layout.ts, etc.) until I actually realised it has something to do with the data returned by the server not being reactive. It was really frustrating to look for an answer because the doc lacks this kind of example and I wonder why, since I believe it’s quite common to want to have certain properties in the data object to be reactive.Also for a beginner it’s already difficult enough to figure out when to use
$state,store,context,$derived,$effectand now I casually should have figured out on my own that I needed a “simple”$statein a$derived.by? Totally unintuitive imo.I’m having the same problem and I’m also unsure how to best handle this. A lot of pages in my SvelteKit app broke because I used something like this to unpack the
dataprop:I want to be able to
I tried a few different things. This one does not work because I can’t update the state locally:
This one does not work because the state does not receive prop updates:
This one does not work because the Svelte compiler gives an error “
$state(...)can only be used as a variable declaration initializer or a class field - svelte(state_invalid_placement)”:But this one does work:
This feels doubly weird, because I can’t use state directly … but I can wrap it in a function and then it’s fine? While this does work, this almost feels like a coincidence. But I think I’ll still go with this for now, as it at least does what I want, is pretty clean and I don’t need the additional
$effectboilerplate.