Replies: 1 comment
|
I think this would be a valuable addition to the docs. For the first question, I prefer the For the second question, I believe this clarification would help many developers migrating from Svelte 4. One thing that often catches people off guard is that reading a value inside a deferred callback ( The Overall, this feels like a small documentation change with a high impact on developer experience, especially for people coming from Svelte 4 or working with asynchronous code. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I’m currently following the official Svelte examples in the Dexie.js documentation, which uses very outdated Svelte3/4 syntax, and converting them to Svelte 5. While doing this, I ran into the issue of tracking state inside deferred async callbacks (like Dexie queries, RxJS observables, or even standard setTimeout calls).
The current Svelte docs for
$effectand$derivedclearly state the limitation (in "Understanding dependencies"):"Values that are read asynchronously — after an await or inside a setTimeout, for example — will not be tracked."
However, they don't explicitly show the idiomatic workaround for when you do need those values tracked. When bridging Svelte's synchronous reactivity with deferred closures, developers have to capture the state synchronously before the async boundary.
Since some others may hit this same wall, I was thinking of opening a PR to add a small clarification to both the
$effectand$derivedsections in the Svelte docs to show this pattern.For instance, in the
$effectsection (after the current setTimeout example), this may be added:To ensure the effect re-runs when an asynchronously read value changes, you must explicitly read it synchronously before the asynchronous boundary. You can do this by using the void operator (our use
watch()in runed library):And for the
$derivedsection (after the untrack explanation):Similarly, if you use $derived.by to return a deferred callback or observable, you must read dependencies synchronously in the main body:
Before I open a PR to the Svelte repo, I wanted to float this here:
void stateToTrack1, stateToTrack2,...;considered the most idiomatic way to express this intent in Svelte 5 without using library like runed'swatch()?(Related conversation on Discord for context: https://discord.com/channels/457912077277855764/1526086042258772109)
All reactions