@rnhooks/async-storage
Version:
React Native hook for AsyncStorage
97 lines (80 loc) • 2.27 kB
JavaScript
import { useState, useEffect } from 'react';
import AsyncStorage from '@react-native-async-storage/async-storage';
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
try {
var info = gen[key](arg);
var value = info.value;
} catch (error) {
reject(error);
return;
}
if (info.done) {
resolve(value);
} else {
Promise.resolve(value).then(_next, _throw);
}
}
function _asyncToGenerator(fn) {
return function () {
var self = this,
args = arguments;
return new Promise(function (resolve, reject) {
var gen = fn.apply(self, args);
function _next(value) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
}
function _throw(err) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
}
_next(undefined);
});
};
}
var index = (function (key) {
var _useState = useState(null),
storageItem = _useState[0],
setStorageItem = _useState[1];
function getStorageItem() {
return _getStorageItem.apply(this, arguments);
}
function _getStorageItem() {
_getStorageItem = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee() {
var data;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return AsyncStorage.getItem(key);
case 2:
data = _context.sent;
setStorageItem(data);
case 4:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return _getStorageItem.apply(this, arguments);
}
function updateStorageItem(data) {
if (typeof data === 'string') {
AsyncStorage.setItem(key, data);
setStorageItem(data);
}
return data;
}
function clearStorageItem() {
AsyncStorage.removeItem(key);
setStorageItem(null);
}
useEffect(function () {
getStorageItem();
}, []);
return [storageItem, updateStorageItem, clearStorageItem];
});
export default index;
//# sourceMappingURL=react-native-hooks-async-storage.esm.js.map