Comments (9)
Hey @jgornick, thanks for the suggestion. Off the top of my head, yes, I think that would be all.
from react-storage-hooks.
Expect a PR soon 😄
from react-storage-hooks.
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.
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.
@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
anduseStorageReducer
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.
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.
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.
@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.
Let's move the conversation into #20 .
from react-storage-hooks.
Related Issues (20)
- Clearing of keys in storages is absent HOT 4
- Could you provide a "sharedSessionStorage" storage object? HOT 11
- React Native window.addEventListener is not a function HOT 1
- SSR hydration warning
- How to invalidate storage after some time (expiration feature)?
- Option to omit specific keys from storage object
- storybook page is not right
- Update Peer dependencies to support React 17 HOT 2
- Infinite Loop in ie11 HOT 1
- Optimize package for different targets
- Using session reducer together with local reducer causes ie11 to freeze up HOT 4
- infinite loop / browser hanging
- Missing sources for sourceMaps
- [BUG] State not syncing with React 17 HOT 4
- Infinite loop on any dispatch HOT 3
- Provide option to omit specific keys from storage HOT 3
- Warning about props not matching after page reload with SSR
- useLocalStorageReducer state by default is undefined HOT 2
- Can't install with React 18
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-storage-hooks.