-
-
Notifications
You must be signed in to change notification settings - Fork 648
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
Cann't access atomWithStorage
value in spa initial
#2240
Comments
The same problem is that the value of storage cannot be obtained at the time of initialization. |
The current solution is to use |
It's our design choice to have |
That said, we should change So, thanks for the feedback. Wish to get more feedback on it. How do you like the name |
Sounds great |
After a lot of confusion I ended up here. I would prefer if the default was |
It's intentionally Would you mind opening a PR to improve the docs? |
I just spent an hour debugging this exact issue - I have a very normal SPA which uses localStorage to store an auth token, migrated it to use This unintuitive behaviour is not even really documented anywhere... truly a terrible first experience with Jotai. @dai-shi could you at least please explain why I think there are no storage items in the entire app which would be usable with Reading between the lines, this seems to be about SSR somehow - but the majority of React apps do not even use SSR, as far as I see. Seems to me that the API is fundamentally broken if the default is only usable in SSR and unusable in SPA (or vice versa, if the default would be switched to true). Perhaps splitting the API behaviour for these two very different use cases would lead to less confusion... |
Thank you for sharing. I share your frustration. I believe the reason is to make it work well with hydration for SSR. But I view that as an opt-in, not the other way around. It would make sense for me if the defaults were tuned for code running in browsers. Most of my projects are SPAs without SSR (bacause content is behind login). |
It was the default behavior in v1 and we received many issues reported. It makes more sense to render initial value first and update with storage value next, and gives less trouble. Which is default can be controversial, but we've been there and done that. |
Was a bit confused by this too, it could be mentioned in the docs that the default is |
Thanks for the feedback! Can you please open a PR to avoid such confusion? |
I'm a jotai newbie, while checking the official example, I found that the persistent state theme blink when refreshing the page (in my local editing environment. It doesn't blink in the playground), by printing I realized that it's because it doesn't get the initial value. But I found that in v1 version it will get the initial value, here is an example using v1: https://codesandbox.io/p/sandbox/brave-mountain-wv2zrt |
See #2391 |
Summary
When I use
atomWithStorage
, the storage already has this value present, but when I usejotaiStore.get(atom)
I only get the initialValue.This is a spa scenario, not an ssr scenario. When atomWithStorage is executed, globally
window
exists.But if I set
unstable_getOnInit
totrue
, it can get the correct value.Link to reproduction
https://codesandbox.io/s/serverless-thunder-s3gddk?file=/src/index.mjs
Check List
Please do not ask questions in issues.
Please include a minimal reproduction.
The text was updated successfully, but these errors were encountered: