Replies: 1 comment 5 replies
-
They can? I also showed this in an example in your recent question. let value = $state(null);
setContext('context', { get value() { return value; } });
// or if you just want to read a value, pass a function:
setContext('context', () => value); |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I believe that many of the problems I have came across are due to states being compiled directly as objects instead of a top level object with the state as the property. ( DISCLAIMER: I do not deeply understand the svelte source code so I don't know if this is actually how the compiler works)
The problems that I encounter are that states passed through imports or contexts cannot properly view the top level value so:
Because objects cannot have functions as their getters we cannot properly pass svelte state through context or imports as the top level value will always be wrong/not update. Instead are only able to access properties as the getters of these are functions which correctly get the values.
If the compiler instead made all instances of the state into an object with the state value as a property this would allow states to work as context objects and as imported objects.
Now I have no idea of how much of a performance impact or how breaking a change like this would be to the project overall. But to me it seems like this is the only way for states to work properly and as expected.
Here is a repl which shows how context doesnt share value properly REPL
To me this seems like svelte should handle state updating in this manner. The point of svelte is to make frontend development just feel like regular javascript and updating objects directly just works, but currently this is one spot where it doesn't work as expected.
(EDIT: I have now read that svelte is using proxies for intercepting the actions of the get, but the same logic still applies as proxies are not allowed to intercept the get of the top level object also)
Beta Was this translation helpful? Give feedback.
All reactions