Setting a component's variable to localStorage / global variable value on the initial render #11137
-
I'm using Svelte + SvelteKit. I'd like to set the initial value of a variable in my component based on the user's previously saved value in
This seems to run once the page has loaded, which in my case means there's a flash of the initial value. I also know I can use
But how can I update my component's value based on this global variable / what's obtained in the For CSS, I can attach a class to the Repl: https://svelte.dev/repl/3e4099fc70814541ae837ff7da50926a?version=4.2.7 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
If you are using ssr (which is on by default) the code is executed on the server where the clients localStorage is not available. Therefore you simply can't achieve it. If you need ssr support you have to use another state method like a cookie or the url. Make sure to read the state in a load function. |
Beta Was this translation helpful? Give feedback.
If you are using ssr (which is on by default) the code is executed on the server where the clients localStorage is not available. Therefore you simply can't achieve it.
If you need ssr support you have to use another state method like a cookie or the url. Make sure to read the state in a load function.