Skip to content

Dynamically creating $state. #15545

Answered by brunnerh
joachimm asked this question in Q&A
Mar 19, 2025 · 1 comments · 1 reply

You must be logged in to vote

Since the $derived returns a plain object, you end up binding to a non-reactive object.
You can create a reactive object within the derived like this:

let values = $derived.by(() => {
  const state = $state(
    taskParameters[selectedTask].map(item => item.defaultValue)
  );

  return state;
});

Currently a separate declaration is required to create a $state object, i.e. it cannot be returned directly.

(By the way, you can add syntax highlighting to code by specifying a language after the first backtick fence, e.g. in your component code ```svelte)

Replies: 1 comment 1 reply

You must be logged in to vote
1 reply
@joachimm

Answer selected by joachimm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants