New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Any way to read the value of a writable? #2060
Comments
AFAIK you'd use |
Aaah - ok. Thanks. I did briefly look at that possibility, and was immediately reminded of John McEnroe remonstrating with an umpire :-) |
Writable stores also have a method |
Yes, I see how (for UI components) this is exactly how you'd want to go. Most observer implementations work along similar lines; but they usually leave a getter in place for components that are not event-driven to be able to inspect state on an ad-hoc basis. Anyway, I'll see what breaks if I extend the writable and add my own getter. |
Something like this is what I meant: const tick = (latestScore) => {
player.update(({ name, highScore }) => {
if (highScore < latestScore) highScore = latestScore;
return { name, highScore };
});
} If you do need to get the current value in a component definition at any moment, and |
Thanks. Inside script tags I've found no issues it's nice and easy to get the value the value. What goes on inside teh script blocks is strong and makes Svelte v cool. I have several parts of an app where the logic is not in script tags but in separate files. In those cases needing to 'update' objects before we can get their value is an interesting design decision. |
This would not work with function get__store(store) {
let $val
store.subscribe($ => $val = $)()
return $val
} |
writing from the future to thank you for |
I store the currently logged user's ID in a readable store. It's easy to imagine that I need to retrieve this user ID repeatedly application-wide.
It's ugly and even my IDE complains about userID not having been initialized. I was thinking, I do not need to subscribe to that value, it will never change except when user logs out. Do I actually need a store? "Subscribing" doesn't make sense here. What are the alternatives? localStorage/sessionStorage. But I do not want to persist the ID, I'd rather keep it in memory. If I persist something, I will do it for a reason, I do not see any strong reason here for me. (user stays logged in through cookies) Then was thinking, I need an additional store, writable and readable don't answer my needs, I need a store that once sets a value and that value will never change again, where I'd have a "get" instead of a "subscribe". I could write such a custom store myself, but the store contract requires a subscribe method. Maybe I should just use the window object? :-/ |
@thojanssens
If you are using in .js file you need to subscribe to the value to get. you cannot use $storeName This will load the script only one time |
I just need to "subscribe once" to this userID value. The store is not a good fit. I don't know why I was so much focused on using stores, I just coded my own kind of store:
In the root .svelte file:
Now that the user ID has been fetched from the server, I can use I have to be careful about the |
you can import |
Wouldn't it be nicer if we could do
I use writable stores a lot on |
@PierBover That wouldn't work on custom stores. Using the already existing |
@kevmodrome because I just want to read a value at a particular time and not trigger reactive logic. Edit:
|
this makes me sad
Can't understand why you can't just use get on the writable. |
I am looking into this and learning. I understand how Is this the reason for separate |
I use my own store function.
Typescript version
|
Thanks, @paganaye, I might end up using this. I'm wondering if anyone can think of some good reasons not to do this, though. |
In case anyone wants to make further modifications to I briefly looked into adding a |
I have an app with a lot nested Svelte stores being processed in non-reactive event handlers, and did what @dpmccabe suggested and copypasted the store implementations and added a It'd be nice to see something like this in the official stores but I just took the shortest path I saw, and I can understand the hesitation to support it. Maybe it's something to address better in the v4 that's been mentioned by the Svelte team. |
Just add $ with the variable name
|
Thank you for this. This makes me very sad too. It costed me 2 hours of my life trying all other possible variants multiple times. Not sure why $x did not work. But at least x.get() should have. since there is a x.set() as well! Finally it is working using above import and a get(x); |
It seems this issue will be solved by svelte 5? The $state rune can be consumed in .svelte files and .js files alike? (I could be wrong though, only briefly looked into runes) |
In my store.js, I may have:
const player = writable({name: 'Player A', highScore: 50});
I may need a function in the store file:
The idea would be that e.g main.js could invoke 'tick' whenever it needed to and pass in a score, which would conditionally update based on a read.
A delightfully simple task...until the the "reading" of the player's highScore. Obvs, binding this value in a visual component works fine. But, short of using OCR to watch my screen, I have not discovered how to extract data from a writable object in code :-)
The text was updated successfully, but these errors were encountered: