$derived
and $effect
not inheriting the component context is a mistake
#13808
WaltzingPenguin
started this conversation in
Ideas
Replies: 1 comment
-
It just now has been fixed for |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In Svelte 4, this code works just fine:
In Svelte 5, this code will run during component initialization but promptly break as soon as
count
changes:I filed it as a bug, someone noted the docs officially say "setContext/getContext must be called during component initialisation.", a maintainer saw that note and then closed the ticket with "Won't Fix". I think this is a mistake and think this should be reversed.
Documentation is inaccurate
First, the documentation is an oversimplification of the actual behaviour. This works just fine in the template for both Svelte 4 & 5:
Utility is greatly diminished
I've seen internationalization, data fetching, user authentication libraries all use the pattern of a top level component for setting up configuration. They then call
getContext
internally in the functions the user is supposed to call. This avoids a global configuration object, a necessity for rendering code on the server without scope pollution. Without this functionality, all the APIs will either be inaccessible inside of$derived
or will need to be rewritten to fetch the configuration options, manually store it, and pass those same configurations to each function called.For a concrete example, imagine a SvelteKit application where information about the current user is stored inside of context. This both allows you to easily preview another user's view and also allows simple functions like
hasPermission(PERMISSION_NAME)
. Transitioning to Svelte 5, that API now to has tohasPermission(user, PERMISSION_NAME)
and I as the developer have to manually track and pass around user, frequently through multiple functions.Error Boundaries will need this anyways
Error Boundaries are supposedly on the roadmap for 5.1. Code executed inside of a
$derived
block should belong to its relevant component so that errors propagate as expected.Beta Was this translation helpful? Give feedback.
All reactions