CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-tarojs--taro-rn

React Native implementation layer for the Taro cross-platform framework, providing React Native-specific APIs and utilities.

Pending
Overview
Eval results
Files

storage.mddocs/

Storage APIs

Local storage management with both synchronous and asynchronous operations for persistent data storage in Taro React Native applications.

Capabilities

Asynchronous Storage Operations

Promise-based storage operations for non-blocking data access.

/**
 * Get data from storage asynchronously
 * @param options Storage get options
 */
function getStorage(options: {
  key: string;
  success?: (res: { data: any }) => void;
  fail?: (res: TaroGeneral.CallbackResult) => void;
  complete?: (res: any) => void;
}): Promise<{ data: any }>;

/**
 * Set data in storage asynchronously
 * @param options Storage set options
 */
function setStorage(options: {
  key: string;
  data: any;
  success?: (res: TaroGeneral.CallbackResult) => void;
  fail?: (res: TaroGeneral.CallbackResult) => void;
  complete?: (res: TaroGeneral.CallbackResult) => void;
}): Promise<TaroGeneral.CallbackResult>;

/**
 * Remove data from storage asynchronously
 * @param options Storage remove options
 */
function removeStorage(options: {
  key: string;
  success?: (res: TaroGeneral.CallbackResult) => void;
  fail?: (res: TaroGeneral.CallbackResult) => void;
  complete?: (res: TaroGeneral.CallbackResult) => void;
}): Promise<TaroGeneral.CallbackResult>;

/**
 * Clear all storage data asynchronously
 * @param options Storage clear options
 */
function clearStorage(options?: {
  success?: (res: TaroGeneral.CallbackResult) => void;
  fail?: (res: TaroGeneral.CallbackResult) => void;
  complete?: (res: TaroGeneral.CallbackResult) => void;
}): Promise<TaroGeneral.CallbackResult>;

Usage Examples:

import { getStorage, setStorage, removeStorage, clearStorage } from "@tarojs/taro-rn";

// Store user data
await setStorage({
  key: 'user',
  data: {
    id: 123,
    name: 'John Doe',
    email: 'john@example.com'
  }
});

// Retrieve user data
const userResult = await getStorage({ key: 'user' });
console.log('User data:', userResult.data);

// Store different data types
await setStorage({ key: 'settings', data: { theme: 'dark', notifications: true } });
await setStorage({ key: 'token', data: 'abc123def456' });
await setStorage({ key: 'count', data: 42 });

// Remove specific item
await removeStorage({ key: 'token' });

// Clear all storage
await clearStorage();

Synchronous Storage Operations

Immediate storage operations that return values directly.

/**
 * Get data from storage synchronously
 * @param key Storage key
 * @returns Stored data or undefined if not found
 */
function getStorageSync(key: string): any;

/**
 * Set data in storage synchronously
 * @param key Storage key
 * @param data Data to store
 */
function setStorageSync(key: string, data: any): void;

/**
 * Remove data from storage synchronously
 * @param key Storage key
 */
function removeStorageSync(key: string): void;

/**
 * Clear all storage data synchronously
 */
function clearStorageSync(): void;

Usage Examples:

import { 
  getStorageSync, 
  setStorageSync, 
  removeStorageSync, 
  clearStorageSync 
} from "@tarojs/taro-rn";

// Store data synchronously
setStorageSync('theme', 'dark');
setStorageSync('user_preferences', {
  language: 'en',
  timezone: 'UTC'
});

// Retrieve data synchronously
const theme = getStorageSync('theme');
const preferences = getStorageSync('user_preferences');

console.log('Theme:', theme);
console.log('Preferences:', preferences);

// Remove item synchronously
removeStorageSync('theme');

// Clear all data synchronously
clearStorageSync();

Storage Information

Get information about storage usage and available space.

/**
 * Get storage information asynchronously
 * @param options Storage info options
 */
function getStorageInfo(options?: {
  success?: (res: {
    keys: string[];
    currentSize: number;
    limitSize: number;
  }) => void;
  fail?: (res: TaroGeneral.CallbackResult) => void;
  complete?: (res: any) => void;
}): Promise<{
  keys: string[];
  currentSize: number;
  limitSize: number;
}>;

/**
 * Get storage information synchronously
 * @returns Storage information
 */
function getStorageInfoSync(): {
  keys: string[];
  currentSize: number;
  limitSize: number;
};

Usage Examples:

import { getStorageInfo, getStorageInfoSync } from "@tarojs/taro-rn";

// Get storage info asynchronously
const storageInfo = await getStorageInfo();
console.log('Stored keys:', storageInfo.keys);
console.log('Current size:', storageInfo.currentSize);
console.log('Limit size:', storageInfo.limitSize);

// Get storage info synchronously
const syncStorageInfo = getStorageInfoSync();
console.log('Available keys:', syncStorageInfo.keys);

// Check if storage is getting full
const usagePercentage = (syncStorageInfo.currentSize / syncStorageInfo.limitSize) * 100;
if (usagePercentage > 80) {
  console.warn('Storage is getting full!');
}

Storage Patterns

Data Types

Storage supports various data types that are automatically serialized:

// Strings
setStorageSync('message', 'Hello World');

// Numbers
setStorageSync('score', 1250);

// Booleans
setStorageSync('isEnabled', true);

// Objects
setStorageSync('config', {
  apiUrl: 'https://api.example.com',
  timeout: 5000,
  retries: 3
});

// Arrays
setStorageSync('items', ['apple', 'banana', 'orange']);

// Complex nested objects
setStorageSync('userData', {
  profile: {
    name: 'John',
    avatar: 'https://example.com/avatar.jpg'
  },
  settings: {
    notifications: {
      email: true,
      push: false
    }
  },
  history: [
    { action: 'login', timestamp: Date.now() },
    { action: 'view_profile', timestamp: Date.now() - 1000 }
  ]
});

Error Handling

Handle storage errors gracefully:

import { getStorage, setStorage } from "@tarojs/taro-rn";

// Using async/await with try-catch
try {
  await setStorage({ key: 'data', data: largeObject });
  console.log('Data stored successfully');
} catch (error) {
  console.error('Storage failed:', error);
}

// Using callback pattern
setStorage({
  key: 'data',
  data: someData,
  success: (res) => {
    console.log('Storage success:', res);
  },
  fail: (error) => {
    console.error('Storage error:', error);
  },
  complete: (res) => {
    console.log('Storage operation completed');
  }
});

Storage Keys Management

Organize storage keys effectively:

// Use consistent naming conventions
const STORAGE_KEYS = {
  USER_TOKEN: 'user_token',
  USER_PROFILE: 'user_profile',
  APP_SETTINGS: 'app_settings',
  CACHE_PREFIX: 'cache_',
  TEMP_PREFIX: 'temp_'
};

// Store with organized keys
setStorageSync(STORAGE_KEYS.USER_TOKEN, 'abc123');
setStorageSync(STORAGE_KEYS.USER_PROFILE, userProfile);
setStorageSync(`${STORAGE_KEYS.CACHE_PREFIX}api_data`, apiResponse);

// Clean up temporary data
const allKeys = getStorageInfoSync().keys;
const tempKeys = allKeys.filter(key => key.startsWith(STORAGE_KEYS.TEMP_PREFIX));
tempKeys.forEach(key => removeStorageSync(key));

Install with Tessl CLI

npx tessl i tessl/npm-tarojs--taro-rn

docs

device-system.md

file-system.md

hooks.md

index.md

location-sensors.md

media.md

navigation.md

network.md

storage.md

ui-apis.md

tile.json