Skip to content

Instantly share code, notes, and snippets.

@dereknahman
Created September 7, 2020 17:17
Show Gist options
  • Save dereknahman/a979100df8122a559ef29b1265039e3c to your computer and use it in GitHub Desktop.
Save dereknahman/a979100df8122a559ef29b1265039e3c to your computer and use it in GitHub Desktop.
import React from 'react';
import { Alert } from 'react-native';
import AsyncStorage from '@react-native-community/async-storage';
const STORAGE_KEY = '@has_stored_value';
// TODO: add the readData in here
// it'll probably complain because we can only have one export default
// find out how/if i can export multiple functions and variables from many functions
//...in a single hook
//TODO: is the data = storedValue ok? Ask Alex later
export default () => {
const [errorMessage, setErrorMessage] = React.useState('');
const saveData = async (data, callback) => {
try {
console.log(data, 'data!!');
const storedData = await AsyncStorage.setItem(STORAGE_KEY, data);
storedData.then(() => {
console.log(storedData, 'testing');
if (callback) {
callback(data);
}
console.log(data, 'DATAAA');
return data;
});
} catch (e) {
setErrorMessage('Language selection storage error');
if (callback) {
callback(null);
}
return null;
}
};
return [saveData, errorMessage];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment