setItem()
Store a string value in storage.
async setItem(key: string, value: string): Promise<void>
Parameters
The string value to store
Returns
A promise that resolves when the value is stored
Example
import Storage from 'expo-native-storage';
await Storage.setItem('username', 'john_doe');
await Storage.setItem('theme', 'dark');
getItem()
Retrieve a string value from storage.
async getItem(key: string): Promise<string | null>
Parameters
The storage key to retrieve
Returns
A promise that resolves to the stored string value, or null if the key doesn’t exist
Example
import Storage from 'expo-native-storage';
const username = await Storage.getItem('username');
if (username) {
console.log(`Welcome back, ${username}!`);
}
// With fallback
const theme = await Storage.getItem('theme') || 'light';
removeItem()
Remove a single item from storage.
async removeItem(key: string): Promise<void>
Parameters
The storage key to remove
Returns
A promise that resolves when the item is removed
Example
import Storage from 'expo-native-storage';
// Remove a single item
await Storage.removeItem('username');
// Clear user session
await Storage.removeItem('authToken');
await Storage.removeItem('userId');
clear()
Remove all items from storage.
async clear(): Promise<void>
Returns
A promise that resolves when all items are removed
Example
import Storage from 'expo-native-storage';
// Clear all storage (e.g., on logout)
await Storage.clear();
This method removes all stored data. Use with caution.
setObject()
Store an object in storage by serializing it to JSON.
async setObject<T extends Record<string, unknown>>(
key: string,
value: T
): Promise<void>
Parameters
value
T extends Record<string, unknown>
required
The object to store (will be serialized to JSON)
Returns
A promise that resolves when the object is stored
Example
import Storage from 'expo-native-storage';
// Store user object
await Storage.setObject('user', {
name: 'John',
age: 30,
premium: true
});
// Store app settings
await Storage.setObject('settings', {
theme: 'dark',
notifications: true,
language: 'en'
});
Objects are serialized using JSON.stringify(), so they must be JSON-serializable.
getObject()
Retrieve an object from storage by deserializing from JSON.
async getObject<T extends Record<string, unknown> = Record<string, unknown>>(
key: string
): Promise<T | null>
Type Parameters
T
Record<string, unknown>
default:"Record<string, unknown>"
The type of the object to retrieve. Defaults to a generic object type.
Parameters
The storage key to retrieve
Returns
A promise that resolves to the deserialized object, or null if:
- The key doesn’t exist
- The stored value is not valid JSON
Example
import Storage from 'expo-native-storage';
// Without type annotation
const user = await Storage.getObject('user');
// With TypeScript type
interface User {
name: string;
age: number;
premium: boolean;
}
const user = await Storage.getObject<User>('user');
if (user) {
console.log(`${user.name} is ${user.age} years old`);
}
Error Handling
If the stored value cannot be parsed as JSON, getObject() returns null instead of throwing an error:
// Safe - returns null if JSON is invalid
const data = await Storage.getObject('corrupted-key');
if (data === null) {
console.log('No valid data found');
}