Overview
TheStorage type defines the interface for persistent state values, while StorageOptions and CreateStorageOptions provide configuration for serialization and storage behavior.
Storage Type
Storage type is a function overload that returns either a typed value or a potentially undefined value, depending on whether an initial value is provided.
Type Signatures
With Initial Value:T.
Without Initial Value:
T | undefined.
StorageOptions
Properties
storageKey (optional)
deserialize (optional)
Date, Map, Set, etc.
Parameters:
value: The string value retrieved from storage
serialize (optional)
deserialize.
Parameters:
value: The typed value to serialize
CreateStorageOptions
createStorage function.
Properties
mmkvInstance (optional)
deserialize (optional)
serialize (optional)
Usage Examples
Basic Storage with Initial Value
Storage Without Initial Value
Custom Storage Key
Custom Serialization for Date
Custom Serialization for Map
Global Serialization with Custom MMKV
Combining Global and Local Options
Type Inference
Stan.js automatically infers types based on the initial value:Best Practices
- Provide Initial Values: Use initial values when possible to avoid dealing with
undefined - Custom Keys: Use descriptive
storageKeyvalues for better debugging - Version Keys: Include version in storage keys when data structure changes
- Type Safety: Ensure
serializeanddeserializeare properly typed - Error Handling: Handle deserialization errors gracefully
- Complex Types: Always provide custom serialization for
Date,Map,Set, etc.
Related
- createStorage() - Function to create storage instances
- Synchronizer - Type for external synchronization
- Persistence Guide - Learn more about persistent storage