Skip to content

Create useAsyncStorage hook #16

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

Closed
michalchudziak opened this issue Feb 22, 2019 · 9 comments
Closed

Create useAsyncStorage hook #16

michalchudziak opened this issue Feb 22, 2019 · 9 comments
Labels
enhancement New feature or request

Comments

@michalchudziak
Copy link

What do you think of creating useAsyncStore hook, that encapsulates the most popular AsyncStorage methods?

const [getItem, setItem] = useAsyncStorage('@NameOfTheStore')

Where setItem(key, value) would save the key in @NameOfTheStore:${key}, and getItem(key) would read from it.

It's just a thought 😅

@sibelius sibelius added the enhancement New feature or request label Feb 22, 2019
@kadikraman
Copy link
Contributor

It would probably have to return an object, since there are 4 possible operations on a single key.

const { getItem, setItem, removeItem, mergeItem } = useAsyncStorage('@NameOfTheStore')

@kadikraman
Copy link
Contributor

Hmm, just realised I didn't read your suggestion properly @michalchudziak - my implementation is for storing and retrieving a single value so the benefit of using the hook would be that you don't need to always pass in the key. Whereas in your suggestion, the API would be identical to the existing one, so it doesn't provide any benefit that I can see.

@krizzu
Copy link
Member

krizzu commented Mar 1, 2019

🎉 This issue has been resolved in version 1.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@krizzu krizzu added the released label Mar 1, 2019
@msukmanowsky
Copy link

@michalchudziak I think this would accomplish what you were originally looking to do https://gist.github.com/msukmanowsky/08a3650223dda8b102d2c9fe94ad5c12

@TheDSCPL
Copy link
Contributor

TheDSCPL commented Jun 5, 2020

@msukmanowsky Kinda... That implementation only detects changes from within the same component's instance.

@msukmanowsky
Copy link

@TheDSCPL true, but that's usually handled by just using this at a top-level App.js and supplying the value to subcomponents via something like Context.

@TheDSCPL
Copy link
Contributor

TheDSCPL commented Jun 5, 2020

Oh, yes! Nice! Imma do that :)

@wesson818
Copy link

wesson818 commented Aug 16, 2020

@msukmanowsky It is great implementation of the useAsyncStorage custom hook. Works perfectly! However, lauchPackager.command gives me a warning: RCTBridge required dispatch_sync to load RCTDevLoadingView. This may lead to deadlocks. Do you have any idea why it happens?

@thomijasir
Copy link

@michalchudziak I have updated and fixed to ensure the code is efficient, the code you shared above will return an infinite loop even on useEffect please check here.
https://gist.github.com/thomijasir/57cef7a26a5d89dad50aaed9c1d72a87

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

8 participants