MCPcopy Create free account
hub / github.com/dabbott/react-native-express / useItem

Function useItem

examples/files/app/useAsyncStorage.js:1–29  ·  view source on GitHub ↗
(key)

Source from the content-addressed store, hash-verified

1export default function useItem(key) {
2 const [item, setItem] = useState(defaultValue)
3
4 async function getValue() {
5 try {
6 const newItem = await AsyncStorage.getItem(key)
7 if (newItem !== item) {
8 setItem(newItem)
9 }
10 } catch (e) {}
11 }
12
13 async function setItem(newItem) {
14 try {
15 if (newItem === null) {
16 await AsyncStorage.removeItem(key)
17 } else {
18 await AsyncStorage.setItem(key, newItem)
19 setItem(newItem)
20 }
21 } catch (e) {}
22 }
23
24 useEffect(() => {
25 getValue()
26 }, [item])
27
28 return [item, setItem]
29}

Callers

nothing calls this directly

Calls 1

getValueFunction · 0.85

Tested by

no test coverage detected