Skip to content
Discussion options

You must be logged in to vote

This is just how variables work in JS: You pass in whatever the variable references at that point in time.
If the variable changes later, the function you passed the value to will not know about it.

The solution is usually to pass a stateful object into the context, that way, the reactivity is contained within the object. Any mutation of properties can be reacted to.

If you need to pass a $derived, you can close over it, e.g.

let value = $derived(...);
setContext('key', () => value);
// or
setContext('key', {
  get value() { return value; }
});

Both of these reference the variable rather than taking the current value directly.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@Ruben-Kruepper
Comment options

@brunnerh
Comment options

Answer selected by Ruben-Kruepper
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