Replies: 1 comment 2 replies
-
|
I'm also discovering these paradigms as I update from Svelte 4 to 5 and search the new docs. It's helpful to have them in one place, thanks. For number 2, I think it works the opposite way though, is that correct? E.g. an update to a store inside a $derived rune triggers an update? |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
These is a collection of non-obvious details about Svelte 5 Runes, they are quite important when working with Runes and are scattered across the docs, GitHub issues/discussions and Reddit or just my own learnings. I update this list from time to time and you are welcome to contribute. You should know the very basics of Runes before reading this list.
$derived: The use of$derivedand$derived.byis quite restricted since it can only be used as a variable declaration initializer or a class field; instead you can just use a function, e.g.() => someRuneState, it works everywhere, often requires less mental overhead and it is way less verbose; do not forget to call the function when using, contrary to$derived; still,$derivedcan be quite handy, in particular as variable declaration initializer in components where I still use itderivedstore (so the one without $), Runes within do not trigger updates; so oldderivedstores need to to be migrated as well (besides, you can't import derived anymore once in Rune mode using the new $derived, except you rename the old derived import)$derived,$derived.by,$effectand within a called function (1); outside of components use the fromStore function.tsfiles to.svelte.tswhen you use Runes, you do not need to do so when importing a function which contains Runes, e.g.,count(a$stateRune) orderivedCount(a function, not a$derivedRune) fromcount.svelte.jscan be imported toanotherFile.tsand used there; so you need to rename only when Runes are used explicitelylet anotherVar = someSpring, you can't do this with primitive $staates where you'd need $derived; while obvious, bugs can be easily introduced, in particular in classes with nested primitive $statesobjectRunes: Check this post which gives a detailed overview, tl;dr is, default to $state objects and functions instead of $derivedSpringclass, check the docs but also this PR for more detailed docs, e.g.,Spring.ofis a handy way to create a derived Spring without the need of the need of$derived$stateas class fields: $state() as class fields turns them into private fields with auto-generated getters and setters, and, e.g., $state.snapshot() uses .toJSON() as part of its functionality, which can’t see private fields; so, you might run into hard-to-detect bugs, more in this Reddit post; this might also affect$derived, see also again (1)Beta Was this translation helpful? Give feedback.
All reactions