I'm writing a react-native app, in which I'm using asyncStorage. Following the recommendations of the docs, I tried using the following wrapper functions:
_storeData = async () => {
try {
await AsyncStorage.setItem('@MySuperStore:key', 'I like to save it.');
} catch (error) {
// Error saving data
}
};
And similar ones for retrieving and deleting keys.
This was working fine, but as you can imagine, these functions are used a lot, so having a copy of them in each file is unmanageable. I created a 'utils.js' file, and rewrote the functions like this:
export async function _storeData(key, data) {
try {
await AsyncStorage.setItem(key, data);
} catch (error) {
console.log("ERROR SAVING");
}
}
And in the code, instead of using this._storeData(key,data)
as before, I used:
import { _storeData } from "../utils";
...
_storeData(key, data)
This does not throw an error, but never seems to actually store, retrieve, or delete anything. I can confirm that when the functions were written as arrow functions within the same file, it worked perfectly.
I'm pretty new to this...
Would it make sense to leave them as arrow functions, and bind them?