-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
The data
prop in +page.svelte
and +layout.svelte
is no longer reactive after migrating to svelte 5
#12999
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
Comments
I guess this could be fixed when Sveltekit would use // +page.svelte
let props = $props();
let data = $derived.by(() => {
let state = $state(props.data);
return state;
}); |
While this works, it's still not intuitive, not documented and migration still breaks apps |
closing as duplicate of sveltejs/svelte#14536 |
I think the trick is to return the Here's a 'real-world' (ish) example: (still need to do the writeup to explain the pieces) You could also use the new class transform feature to do it directly from the server load fn. |
<script>
let props = $props()
let data = $state(props.data)
$effect(() => {
data = props.data;
});
</script> A variation of 3.2 that applies to all sveltejs/svelte#15142 (comment) maintainers confirmation of Patricks pattern |
For anyone who ends up here. Writable derived is now built-in. |
This only solves half to he problem, since |
Describe the bug
$effect
is discouraged, yet it is the only viable solution for handling updates that come from both the client and server. This is neither mentioned nor explained in the migration or core documentation.Suggestions
$effect
is appropriateStep 1 - Svelte 4 Reactivity
This is a typical reactive setup with local state update for optimistic UI.
Step 2 - Migrate to svelte 5 via
npx sv migrate svelte-5
The CLI replaced
export let
with$props
, but we're no longer able to get reactive updates fordata.value
.$bindable()
was also added for no observable effect.Step 3 - Use
$state
Local updates now work, but we're no longer able to receive updates from the server.
Step 3.1 - Use
$derived
Cannot assign to derived state
. :|Step 3.2 - Use
$state
+$effect
Finally, we're able to return to the svelte 4 behaviour. This is also the solution @benmccann suggests in sveltejs/svelte#14536. But this solution is a problem, as he puts it.
On top of that step 3 and 3.1 were intuitive solutions a user will reach for, yet it is incorrect.
Severity
blocking an upgrade
Additional Information
Global State
I think there is also a high correlation between this and setting up a global state, as users often will and are encouraged to set up their data in the load fn and would like to use them in various pages. I created a second issue that addresses that in #13000.
Related discussions from maintainers
$props()
svelte#14536Related help threads
The text was updated successfully, but these errors were encountered: