Async-storage: Create `useAsyncStorage` hook

Created on 22 Feb 2019  路  8Comments  路  Source: react-native-async-storage/async-storage

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 馃槄

enhancement

Most helpful comment

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

All 8 comments

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

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.

:tada: This issue has been resolved in version 1.1.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

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

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

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

Oh, yes! Nice! Imma do that :)

@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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

loic-lopez picture loic-lopez  路  37Comments

Waqas-Jani picture Waqas-Jani  路  28Comments

cpojer picture cpojer  路  34Comments

michaelsinatra picture michaelsinatra  路  24Comments

while-it picture while-it  路  51Comments