Giter Site home page Giter Site logo

Comments (14)

jaens avatar jaens commented on May 27, 2024 3

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 atomWithStorage thinking it would be a drop-in, and suddenly the first render of a component always reads the uninitialized value for some reason (which causes the component to throw an exception, since it needs it to start fetching data...).

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 getOnInit is not the default for storage atoms? "more people would get confused?" Confused about what? I am pretty confused about the way it is right now... How is reading an uninitialized value, but only on the first render anything besides confusing?

I think there are no storage items in the entire app which would be usable with getOnInit: false...

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...

from jotai.

dai-shi avatar dai-shi commented on May 27, 2024 1

That said, we should change unstable_getOnInit to getOnInit some time soon.

So, thanks for the feedback. Wish to get more feedback on it. How do you like the name getOnInit?

from jotai.

jiangmaniu avatar jiangmaniu commented on May 27, 2024

The same problem is that the value of storage cannot be obtained at the time of initialization.

from jotai.

Innei avatar Innei commented on May 27, 2024

The same problem is that the value of storage cannot be obtained at the time of initialization.

The current solution is to use unstable_getOnInit: true. I think getOnInit should be a reasonable behavior in non-ssr scenarios.

from jotai.

dai-shi avatar dai-shi commented on May 27, 2024

It's our design choice to have getOnInit false by default.

from jotai.

Innei avatar Innei commented on May 27, 2024

That said, we should change unstable_getOnInit to getOnInit some time soon.

So, thanks for the feedback. Wish to get more feedback on it. How do you like the name getOnInit?

Sounds great

from jotai.

ViktorQvarfordt avatar ViktorQvarfordt commented on May 27, 2024

After a lot of confusion I ended up here. I would prefer if the default was true. At least I would expect this to be clearly documented an not hidden. In the docs no default is described. Cheers!

from jotai.

dai-shi avatar dai-shi commented on May 27, 2024

It's intentionally false by default. Otherwise, more people would get confused. (which was the case in v1).

Would you mind opening a PR to improve the docs?

from jotai.

ViktorQvarfordt avatar ViktorQvarfordt commented on May 27, 2024

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).

from jotai.

dai-shi avatar dai-shi commented on May 27, 2024

could you at least please explain why getOnInit is not the default for storage atoms?

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.

from jotai.

henrikvilhelmberglund avatar henrikvilhelmberglund commented on May 27, 2024

Was a bit confused by this too, it could be mentioned in the docs that the default is false.

from jotai.

dai-shi avatar dai-shi commented on May 27, 2024

Thanks for the feedback! Can you please open a PR to avoid such confusion?

from jotai.

zhylmzr avatar zhylmzr commented on May 27, 2024

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

Screenshot

PixPin_2024-02-16_21-19-45

from jotai.

dai-shi avatar dai-shi commented on May 27, 2024

See #2391

from jotai.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.