Skip to content

Extracting Nested Properties from an External Svelte Store: Legacy Derived vs. $derived() Runes API #15202

Answered by brunnerh
kevin82222 asked this question in Q&A
Discussion options

You must be logged in to vote

You can use $derived with stores via $store syntax, but you can only use $store within components. You can use fromStore as noted by @webJose outside of components (in .svelte.js/ts files), the object returned from that then can be used in $derived (or other reactive contexts).

For extracting values I would use $derived in components or just a function when in an external module. $derived cannot be exported directly, so you need some wrapper, having a $derived in-between has little value.

E.g.

const pageState = fromStore(page);

export function user() { return pageState.current.user; }

Playground

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by kevin82222
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants