GithubHelp home page GithubHelp logo

Comments (9)

soyguijarro avatar soyguijarro commented on May 18, 2024

Hey @jgornick, thanks for the suggestion. Off the top of my head, yes, I think that would be all.

from react-storage-hooks.

jgornick avatar jgornick commented on May 18, 2024

Expect a PR soon 😄

from react-storage-hooks.

jgornick avatar jgornick commented on May 18, 2024

Started to work on the change here: https://github.com/jgornick/react-storage-hooks/blob/14-add-async-storage-support/src/common.ts

Any feedback or help would be greatly appreciated 😄

from react-storage-hooks.

soyguijarro avatar soyguijarro commented on May 18, 2024

Started to work on the change here: https://github.com/jgornick/react-storage-hooks/blob/14-add-async-storage-support/src/common.ts

Any feedback or help would be greatly appreciated smile

Looking good at a quick glance! Feel free to open a PR if you want and I'll take a closer look, but please make sure to add some test cases to cover the new async behavior. Thanks!

from react-storage-hooks.

jgornick avatar jgornick commented on May 18, 2024

@soyguijarro Looking at this a bit more, this is a bit more difficult than I originally thought.

A couple of concerns:

  • Since our storage methods are now async, this means that the external API for methods like useStorageState and useStorageReducer will most likely change. I'm not a hooks expert, but based on other async related hooks, the result includes a "loading" flag to know when the state is available to be read and updated.
  • Again, now that everything is async, we would have to wrap most, if not all, of the hooks publicly and privately with useEffect. Also, similar to the first concern, we would have to update the APIs to support a "loading" flag of sorts.

Again, I must admit, I'm not a hooks expert. However, the more I dig into this, the more it seems like a breaking change more than anything. If I'm not far off on this, as the owner of the library, are you OK with breaking API changes? Do we split out async support into their own hooks?

Thoughts?

from react-storage-hooks.

jgornick avatar jgornick commented on May 18, 2024

Just as an update, I did start to look at ways to implement useEffect as means to handle async storage methods: https://github.com/jgornick/react-storage-hooks/blob/14-add-async-storage-support/src/state.ts#L21-L37

The change to the behavior of the hook then is that the state returned in the tuple will be null to start, then after the useEffect is called and the promise for reading the item from storage, the state will be updated.

I also started to modify the tests for the hook as well: https://github.com/jgornick/react-storage-hooks/blob/14-add-async-storage-support/src/tests/state.test.ts#L16-L27. They use the waitForNextUpdate method returned from the renderHook test utility.

Any feedback would be greatly appreciated!

from react-storage-hooks.

soyguijarro avatar soyguijarro commented on May 18, 2024

You're totally right @jgornick, this would be a breaking change, but I'm fine with that. I think having async support would solve this SSR issue as well, so it's probably worth doing.

Having sync and async versions of the hooks might look like it simplifies the sync use case, but it introduces complexity for users anyway: you now need to choose between the two hook flavors which have slightly different APIs, switch between them if you change your storage... Not worth it IMHO. I'd go with converting everything to async. For consumers using sync storages like localStorage, if your application doesn't break with the initial undefined you might not even care about the loading state since it'll be super short.

from react-storage-hooks.

jgornick avatar jgornick commented on May 18, 2024

@soyguijarro I'm not quite sure I know exactly what I'm doing, but I've made a commit that shows the updated interface for the state hook. I've also started to make some changes to the reducer hook, but not finished.

Any chance you can have a look so far with what's there and if I should continue with updating tests?

Thanks!

from react-storage-hooks.

jgornick avatar jgornick commented on May 18, 2024

Let's move the conversation into #20 .

from react-storage-hooks.

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.