this is my first time trying a custom AsyncStorage Hook for a project, but I keep getting this LOG Error: Error: [AsyncStorage] Passing null/undefined as value is not supported. If you want to remove value, Use .removeItem method instead.
Passed value: undefined
Passed key: budgets
] This Hook is meant to store the budgets and expenses in my context file
The AsyncStorage Hooks are:
import { useEffect, useState } from 'react';
import AsyncStorage from '@react-native-async-storage/async-storage';
const useAsyncStorage = (key, defaultValue) => {
const [storedValue, setStoredValue] = useState(null);
useEffect(() => {
async function getStoredValue() {
try {
const value = await AsyncStorage.getItem(key);
if (value !== null) {
setStoredValue(value);
} else if (defaultValue !== undefined) {
setStoredValue(defaultValue);
} else if (typeof defaultValue === 'function') {
setStoredValue(defaultValue());
}
} catch (e) {
console.log(`Error getting ${key} from AsyncStorage`, e);
}
}
getStoredValue();
}, [key]);
const setValue = async (value) => {
try {
await AsyncStorage.setItem(key, value);
setStoredValue(value);
} catch (e) {
console.log(`Error setting ${key} in AsyncStorage`, e);
}
};
return [storedValue, setValue];
};
export default useAsyncStorage;
This is where it’s used:
const [budgets, setBudgets] = useAsyncStorage("budgets",[]);
const [expenses, setExpenses] = useAsyncStorage("expenses",[]);
I would appreciate the help thanks. Please if the code isn’t clear or I need to provide more details let me know
2
Answers
Not to worry, I've solved it. I had an error in my useAsyncStorage hook just had to change the hook to this: