Storage adapter for Natify Framework using `@react-native-async-storage/async-storage`.
npm install @natify/storage-asyncStorage adapter for Natify Framework using @react-native-async-storage/async-storage.
``bash`
pnpm add @natify/storage-async @react-native-async-storage/async-storage
| Adapter | Use Case |
|---------|----------|
| storage-async | Non-sensitive data, maximum compatibility |
| storage-mmkv | High performance, frequent data access |
| storage-keychain | Sensitive data (tokens, passwords) |
AsyncStorage is ideal for:
- User preferences
- Data cache
- Onboarding state
- App configurations
DO NOT use for:
- Authentication tokens (use Keychain)
- Passwords
- Sensitive data
`typescript
import { NatifyProvider } from "@natify/core";
import { AsyncStorageAdapter } from "@natify/storage-async";
const config = {
storage: new AsyncStorageAdapter(),
// ... other adapters
};
function App() {
return (
);
}
`
`typescript
import { useAdapter, StoragePort } from "@natify/core";
function SettingsScreen() {
const storage = useAdapter
// Save preferences
const saveTheme = async (theme: "light" | "dark") => {
await storage.setItem("user_theme", theme);
};
// Read preferences
const loadTheme = async () => {
const theme = await storage.getItem
return theme || "light";
};
// Save complex object
const saveUserPreferences = async (prefs: UserPreferences) => {
await storage.setItem("user_preferences", prefs);
};
// Read complex object
const loadUserPreferences = async () => {
const prefs = await storage.getItem
return prefs || defaultPreferences;
};
}
`
`typescript
function OnboardingCheck() {
const storage = useAdapter
const [showOnboarding, setShowOnboarding] = useState(true);
useEffect(() => {
checkOnboarding();
}, []);
const checkOnboarding = async () => {
const completed = await storage.getItem
setShowOnboarding(!completed);
};
const completeOnboarding = async () => {
await storage.setItem("onboarding_completed", true);
setShowOnboarding(false);
};
if (showOnboarding) {
return
}
return
}
`
`typescript
interface CachedData
data: T;
timestamp: number;
expiresIn: number;
}
function useCachedData
const storage = useAdapter
const getData = async (): Promise
const cached = await storage.getItem);
if (cached && Date.now() - cached.timestamp < cached.expiresIn) {
return cached.data;
}
const freshData = await fetcher();
await storage.setItem, {
data: freshData,
timestamp: Date.now(),
expiresIn: ttl,
});
return freshData;
};
return { getData };
}
`
| Method | Return | Description |
|--------|--------|-------------|
| getItem | Promise | Gets a value by key |setItem
| | Promise | Saves a value |removeItem(key)
| | Promise | Removes a value |clear()
| | Promise | Clears all storage |
The adapter automatically serializes/deserializes:
- stringnumber
- boolean
- object
- (JSON serializable)array` (JSON serializable)
-
- Asynchronous: All operations are async (non-blocking)
- Performance: Slower than MMKV for frequent operations
- No encryption: Data is not encrypted (use Keychain for sensitive data)
- Size limit: ~6MB on Android, no limit on iOS